c#中dataset导出到excel
cn = new adodb.connection
str = "provider=microsoft.jet.oledb.4.0;data source=" & me.ofdselectexcel.filename & ";extended properties=""excel 8.0;hdr=yes"""
cn.open(str)
'获取所有sheet的名称
cbsheet.properties.items.clear()
dim rs1 as new adodb.recordset
rs1 = cn.openschema(adodb.schemaenum.adschematables)
while not rs1.eof
cbsheet.properties.items.add(rs1.fields("table_name").value)
rs1.movenext()
end while
cn.close()
////上面是获取选中excel的sheet名称
出错是因为你的excel的sheet名称被改过了,默认是sheet1$
好了给合上面我们来看个完整的dataset导出到excel代码。
/// <summary>
/// 将datatable的数据导出到excel中。
/// </summary>
/// <param name="dt">datatable</param>
/// <param name="xlsfiledir">导出的excel文件存放目录(绝对路径,最后带“”)</param>
/// <param name="namelist">datatable中列名的中文对应表</param>
/// <param name="strtitle">excel表的标题</param>
/// <returns>excel文件名</returns>
public static string exportdatatoexcel(system.data.datatable dt, string xlsfiledir, hashtable namelist, string strtitle)
{
if (dt == null) return "";microsoft.office.interop.excel.applicationclass excel = new microsoft.office.interop.excel.applicationclass();
microsoft.office.interop.excel.workbooks workbooks = excel.workbooks;
microsoft.office.interop.excel.workbook workbook = workbooks.add(microsoft.office.interop.excel.xlwbatemplate.xlwbatworksheet);
microsoft.office.interop.excel.worksheet worksheet = (microsoft.office.interop.excel.worksheet)workbook.worksheets[1];int titlerowscount = 0;
if (strtitle != null && strtitle.trim() != "")
{
titlerowscount = 1;
excel.get_range(excel.cells[1, 1], excel.cells[1, dt.columns.count]).font.bold = true;
excel.get_range(excel.cells[1, 1], excel.cells[1, dt.columns.count]).font.size = 16;
excel.get_range(excel.cells[1, 1], excel.cells[1, dt.columns.count]).mergecells = true;
worksheet.cells[1, 1] = strtitle;
}
if (!system.io.directory.exists(xlsfiledir))
{
system.io.directory.createdirectory(xlsfiledir);
}
string strfilename = datetime.now.tostring("yyyymmddhhmmssff") + ".xls";string tempcolumnname = "";
for (int i = 0; i < dt.rows.count; i++)
{
for (int j = 0; j < dt.columns.count; j++)
{
if (i == 0)
{
tempcolumnname = dt.columns[j].columnname.trim();
if (namelist != null)
{
idictionaryenumerator enum = namelist.getenumerator();
while (enum.movenext())
{
if (enum.key.tostring().trim() == tempcolumnname)
{
tempcolumnname = enum.value.tostring();
}
}
}
worksheet.cells[titlerowscount + 1, j + 1] = tempcolumnname;
}
worksheet.cells[i + titlerowscount + 2, j + 1] = dt.rows[i][j].tostring();
}
}
excel.get_range(excel.cells[titlerowscount + 1, 1], excel.cells[titlerowscount + 1, dt.columns.count]).font.bold = true;
excel.get_range(excel.cells[1, 1], excel.cells[titlerowscount + 1 + dt.rows.count, dt.columns.count]).horizontalalignment = xlvalign.xlvaligncenter;
excel.get_range(excel.cells[1, 1], excel.cells[titlerowscount + 1 + dt.rows.count, dt.columns.count]).entirecolumn.autofit();workbook.saved = true;
workbook.savecopyas(xlsfiledir + strfilename);
system.runtime.interops教程ervices.marshal.releasecomobject(worksheet);
worksheet = null;
system.runtime.interopservices.marshal.releasecomobject(workbook);
workbook = null;
workbooks.close();
system.runtime.interopservices.marshal.releasecomobject(workbooks);
workbooks = null;
excel.quit();
system.runtime.interopservices.marshal.releasecomobject(excel);
excel = null;
return strfilename;
}