tab 1
此 demo 展示如何把数据库中的数据读取到 datatable,并将 datatable 导入到 excel。
如果这不是您想要的 demo,您可以通过填写表格获取免费定制 demo。
如您有与我们产品相关的其他技术问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。;销售相关的问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。。
tab 2
using system.data.oledb;
using system.data;
using spire.xls;
namespace demoonlinecode
{
class exportdatatabletoexcelfromdatabase
{
public void demoexportdatatabletoexcel(string databasepath, string tablename,string resultname)
{
workbook book = new workbook();
worksheet sheet = book.worksheets[0];
datatable datatable = exportdatatable(databasepath, tablename);
sheet.insertdatatable(datatable, true, 1, 1);
sheet.name = tablename;
book.savetofile(resultname,excelversion.version2010);
}
private datatable exportdatatable(string databasepath, string tablename)
{
oledbconnection connection = new oledbconnection();
connection.connectionstring = @"provider=""microsoft.jet.oledb.4.0"";data source=" databasepath ";user id=;password=";
oledbcommand command = new oledbcommand();
command.commandtext = "select * from " tablename;
dataset dataset = new system.data.dataset();
oledbdataadapter dataadapter = new oledbdataadapter(command.commandtext, connection);
dataadapter.fill(dataset);
datatable datatable = dataset.tables[0];
return datatable;
}
}
}
tab 3
imports system.data.oledb
imports system.data
imports spire.xls
namespace demoonlinecode
class exportdatatabletoexcelfromdatabase
public sub demoexportdatatabletoexcel(databasepath as string, tablename as string, resultname as string)
dim book as new workbook()
dim sheet as worksheet = book.worksheets(0)
dim datatable as datatable = exportdatatable(databasepath, tablename)
sheet.insertdatatable(datatable, true, 1, 1)
sheet.name = tablename
book.savetofile(resultname, excelversion.version2010)
end sub
private function exportdatatable(databasepath as string, tablename as string) as datatable
dim connection as new oledbconnection()
connection.connectionstring = "provider=""microsoft.jet.oledb.4.0"";data source=" & databasepath & ";user id=;password="
dim command as new oledbcommand()
command.commandtext = "select * from " & tablename
dim dataset as dataset = new system.data.dataset()
dim dataadapter as new oledbdataadapter(command.commandtext, connection)
dataadapter.fill(dataset)
dim datatable as datatable = dataset.tables(0)
return datatable
end function
end class
end namespace