spire.dataexport支持将数据从数据源如数据库, listview 和 datatable 等导出到 word, excel,rtf, access, pdf, xps,html, xml, text,csv, dbf, sylk,sql server,dif 和剪贴板等格式。该文章将详细介绍如何使用spire.dataexport 从datatable导出数据到excel, word, pdf, html 等几种常用文件。
第一步:在从datatable将数据导出到文件前,我们首先需要连接数据库,从数据源获得数据并生成一个datatable. 获取需要导出的数据.
c#
// office 07及以上版本
string connectionstring = "provider=microsoft.ace.oledb.12.0;data source=data.accdb";
//// office 07以下版本
//string connstring = provider=microsoft.jet.oledb.4.0;data source= path "
oledbcommand oledbcommand = new oledbcommand(connectionstring);
oledbcommand.commandtext = "select * from [sheet1]";
oledbdataadapter ada = new oledbdataadapter(oledbcommand.commandtext, connectionstring);
dataset set = new dataset();
ada.fill(set);
dataset dataset = new system.data.dataset();
oledbcommand.connection = oledbconnection;
datatable datatable = set.tables[0];
vb.net
' office 07及以上版本
dim connectionstring as string = "provider=microsoft.ace.oledb.12.0;data source=data.accdb"
' office 07以下版本
'string connstring = provider=microsoft.jet.oledb.4.0;data source= path "
dim oledbcommand as new oledbcommand(connectionstring)
oledbcommand.commandtext = "select * from [sheet1]"
dim ada as new oledbdataadapter(oledbcommand.commandtext, connectionstring)
dim [set] as new dataset()
ada.fill([set])
dim dataset as dataset = new system.data.dataset()
oledbcommand.connection = oledbconnection
dim datatable as datatable = [set].tables(0)
第二步:将数据从datatable导出到文件。spire.dataexport支持将数据导出到word, excel,rtf,ms access, pdf, xps,html, xml, text,csv, dbf, sylk,sql server,dif 和剪贴板等格式文件。这里我们以word, excel, pdf, html, ms access五种格式为例。
从datatable导出数据到excel
c#
spire.dataexport.xls.cellexport cellexport = new spire.dataexport.xls.cellexport();
spire.dataexport.xls.worksheet worksheet1 = new spire.dataexport.xls.worksheet();
worksheet1.datasource = spire.dataexport.common.exportsource.datatable;
worksheet1.datatable = datatable;
worksheet1.startdatacol = ((system.byte)(0));
worksheet1.autofitcolwidth = true;
cellexport.sheets.add(worksheet1);
cellexport.actionafterexport = spire.dataexport.common.actiontype.openview;
cellexport.savetofile("datatoexcel.xls");
vb.net
dim cellexport as new spire.dataexport.xls.cellexport()
dim worksheet1 as new spire.dataexport.xls.worksheet()
worksheet1.datasource = spire.dataexport.common.exportsource.datatable
worksheet1.datatable = datatable
worksheet1.startdatacol = cbyte(0)
worksheet1.autofitcolwidth = true
cellexport.sheets.add(worksheet1)
cellexport.actionafterexport = spire.dataexport.common.actiontype.openview
cellexport.savetofile("datatoexcel.xls")
从datatable导出数据到rtf, 并将rtf存为word格式
c#
spire.dataexport.rtf.rtfexport rtfexport = new spire.dataexport.rtf.rtfexport();
rtfexport.datasource = spire.dataexport.common.exportsource.datatable;
rtfexport.datatable = datatable;
rtfexport.actionafterexport = spire.dataexport.common.actiontype.openview;
rtfstyle rtfstyle = new rtfstyle();
rtfstyle.fontcolor = color.blue;
rtfstyle.backgroundcolor = color.lightgreen;
rtfexport.rtfoptions.datastyle = rtfstyle;
rtfexport.filename = "datatoword.doc";
rtfexport.savetofile();
vb.net
dim rtfexport as new spire.dataexport.rtf.rtfexport()
rtfexport.datasource = spire.dataexport.common.exportsource.datatable
rtfexport.datatable = datatable
rtfexport.actionafterexport = spire.dataexport.common.actiontype.openview
dim rtfstyle as new rtfstyle()
rtfstyle.fontcolor = color.blue
rtfstyle.backgroundcolor = color.lightgreen
rtfexport.rtfoptions.datastyle = rtfstyle
rtfexport.filename = "datatoword.doc"
rtfexport.savetofile()
从datatable导出数据到pdf
c#
spire.dataexport.pdf.pdfexport pdfexport = new spire.dataexport.pdf.pdfexport();
pdfexport.datasource = spire.dataexport.common.exportsource.datatable;
pdfexport.datatable = datatable;
pdfexport.dataencoding = spire.dataexport.pdf.pdfencodingtype.utf8;
pdfexport.actionafterexport = spire.dataexport.common.actiontype.openview;
pdfexport.savetofile("datatopdf.pdf");
vb.net
dim pdfexport as new spire.dataexport.pdf.pdfexport()
pdfexport.datasource = spire.dataexport.common.exportsource.datatable
pdfexport.datatable = datatable
pdfexport.dataencoding = spire.dataexport.pdf.pdfencodingtype.utf8
pdfexport.actionafterexport = spire.dataexport.common.actiontype.openview
pdfexport.savetofile("datatopdf.pdf")
从datatable导出数据到html
c#
spire.dataexport.html.htmlexport htmlexport = new spire.dataexport.html.htmlexport();
htmlexport.datasource = spire.dataexport.common.exportsource.datatable;
htmlexport.datatable = datatable;
htmlexport.actionafterexport = spire.dataexport.common.actiontype.openview;
htmlexport.savetofile("datatohtml.html");
vb.net
dim htmlexport as new spire.dataexport.html.htmlexport()
htmlexport.datasource = spire.dataexport.common.exportsource.datatable
htmlexport.datatable = datatable
htmlexport.actionafterexport = spire.dataexport.common.actiontype.openview
htmlexport.savetofile("datatohtml.html")
从datatable导出数据到ms access
c#
spire.dataexport.access.accessexport accessexport = new
spire.dataexport.access.accessexport();
accessexport.datasource = spire.dataexport.common.exportsource.datatable;
accessexport.datatable = datatable;
accessexport.databasename = "tomdb.mdb";
accessexport.tablename = "exportfromdatatable";
accessexport.savetofile();
vb.net
dim accessexport as new spire.dataexport.access.accessexport()
accessexport.datasource = spire.dataexport.common.exportsource.datatable
accessexport.datatable = datatable
accessexport.databasename = "tomdb.mdb"
accessexport.tablename = "exportfromdatatable"
accessexport.savetofile()