参数解释:dt--导出的表7a6" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>asp.net</em>三个datatable导出到同一个excel文...

<em>asp.net</em>三个datatable导出到同一个excel文...

作者:乔山办公网日期:

返回目录:excel表格制作


你搜索并参考 datagridview导出到excel
原理是一样 的

参数解释:dt--导出的表7a686964616fe4b893e5b19e331。 toFileName入到的Execl。 strSheetName Execl中的sheet名。
private void doExport(DataTable dt, string toFileName, string strSheetName)
{

Excel.Application excel = new Excel.Application(); //Execl的操作类
//读取保存目标的对象
Excel.Workbook bookDest = excel.Workbooks._Open(toFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//打开要导出到的Execl文件的工作薄。--ps:关于Missing类在这里的作用,我也不知道...囧

Excel.Worksheet sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;//给工作薄添加一个Sheet

sheetDest.Name = strSheetName;//自己定义名字O(∩_∩)O哈哈~

int rowIndex = 1;
int colIndex = 0;

excel.Application.Workbooks.Add(true);//这句不写不知道会不会报错

foreach (DataColumn col in dt.Columns)
{
colIndex++;
sheetDest.Cells[1, colIndex] = col.ColumnName;//Execl中的第一列,把DataTable的列名先导进去
}

//导入数据行
foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dt.Columns)
{
colIndex++;
sheetDest.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}

bookDest.Saved = true;

bookDest.Save();
excel.Quit();
excel = null;

GC.Collect();//垃圾回收
}

不知道你能理解吗,这个方法是肯定可行的。我试过
有不懂的地方可以问我
创建3个workbook对象分别把他们导出到这三个中

比如:
var aplExcel = newApplication {DisplayAlerts = false, SheetsInNewWorkbook = 3};
var workBook = (_Workbook)aplExcel.Workbooks.Add(Missing.Value);
var activateSheet = (_Worksheet)workBook.Sheets[1];
activateSheet.Activate();
workBook.SaveAs(excelFilePath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

workBook.Close();
可以创建出来一个excel再把数据贴上去,再按你要的要求循e69da5e6ba907a64361环三次

//dataset导出EXCEL
public void CreateExcel(DataSet ds, string FileType, string FileName)
{
//try
//{
//HttpResponse resp;
//resp = Page.Response;
Response.Clear();
Response.Charset = "UTF-8";
Response.Buffer = true;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");

Response.ContentType = FileType;

string colHeaders = string.Empty;
string ls_item = string.Empty;

//定义表对636f7079e799bee5baa6e997aee7ad94330象与行对象,同时用DataSet对其值进行初始化
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;

//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{

if (i == (cl - 1))//最后一列,加n
{
colHeaders += ColChange(dt.Columns[i].Caption.ToString()) + "\n";
}
else
{
colHeaders += ColChange(dt.Columns[i].Caption.ToString()) + "\t";
}
}
Response.Output.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息

//逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}

}
Response.Output.Write(ls_item);
ls_item = string.Empty;

}

Response.Output.Flush();
Response.End();
//}
//catch
//{
//}
}

//调用函数
CreateExcel(ds, "application/ms-excel", ”到处的文件名“);

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网