惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
宝玉的分享
宝玉的分享
I
InfoQ
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
V
V2EX
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
T
The Blog of Author Tim Ferriss
量子位

博客园 - 空空儿

硬盘分区、寻址和系统启动过程 JavaScript里String.Format方法的实现 asp.net异步页 获取SQL Server数据行的物理地址信息(%%lockress%% & %%physloc%%) 使用XML的value()方法将多行数据合并成一列 获取所有的外键信息 ASP.NET application and page life cycle SQL Server 2005 CLR 调用Web Service需要注意的几个问题 SQL SERVRE 2005 CLR TVF错误:从用户定义的表值函数获取新行时出错:Data access is not allowed in this context. PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 在C#里使用属性 - 空空儿 - 博客园 Read text file (txt, csv, log, tab, fixed length) 批量写数据---将XML数据批量写入数据库 对对象类型和调用方法属性进行存储以提升反射性能 过程 sp_addlinkedsrvlogin,第 91 行解密过程中出错的解决办法 生成大量随机字符串不同实现方式的效率对比 用SQL SERVER 2005新提供的命令实现行列转换 APM (异步编程模型) 利用宏让ERStudio生成代码文件
利用Spire.DataExport将数据(Database/DataTable)导成各种文件
空空儿 · 2011-03-13 · via 博客园 - 空空儿

借助Spire.DataExport可以很方便的将Database或者DataTable里的数据导成XLS, PDF and MS Word, HTML, MS ,XML, PDF, DBF, SQL Script, SYLK, DIF, CSV文件或者剪切板里而且机器上并不需要安装Microsoft Excel ,Microsoft Access ,Adobe Acrobat.转换后的数据可以输出到File,HttpResponse,Stream里.

当前的免费Spire.DataExport对日期类型的数据存在一个Bug:导出的数据会丢失时间部分,这个问题在以后的版本可能会解决掉,在没修正这个Bug前需要采取别的措施,比喻将时间类型转换成字符串类型.

如果要从Database里查询数据并输出成文件,需要指定相应的导出格式对象实例的DataSource属性为ExportSource.SqlCommand,并为Columns和SQLCommand属性赋相应的值.如果直接将DataTable输出成文件,则需要指定DataSource属性为ExportSource.DataTable.DataSource的默认值为ExportSource.SqlCommand.

要用Spire.DataExport组件,项目里需要引用Spire.License.dll,Spire.DataExport.ResourceMgr.dll,Spire.DataExport.dll.

以下示例将一个DataTable输出成XLS文件:

static void ExportToXLS(DataTable dbTable, string strFileName)
{
       CellExport cellExport 
= new CellExport();

        cellExport.ActionAfterExport 

= ActionType.None;
        cellExport.AutoFitColWidth 
= true;
        cellExport.DataFormats.CultureName 
= "en-US";
        cellExport.DataFormats.Currency 
= "#,###,##0.00";
        cellExport.DataFormats.DateTime 
= "yyyyhhmm HHmmss";
        cellExport.DataFormats.Float 
= "#,###,##0.00";
        cellExport.DataFormats.Integer 
= "#,###,##0";
        cellExport.DataFormats.Time 
= "H:mm";
        cellExport.SheetOptions.AggregateFormat.Font.Name 
= "Arial";
        cellExport.SheetOptions.CustomDataFormat.Font.Name 
= "Arial";
        cellExport.SheetOptions.DefaultFont.Name 
= "Arial";
        cellExport.SheetOptions.FooterFormat.Font.Name 
= "Arial";
        cellExport.SheetOptions.HeaderFormat.Font.Name 
= "Arial";
        cellExport.SheetOptions.HyperlinkFormat.Font.Color 
= CellColor.Blue;
        cellExport.SheetOptions.HyperlinkFormat.Font.Name 
= "Arial";
        cellExport.SheetOptions.HyperlinkFormat.Font.Underline 
= XlsFontUnderline.Single;
        cellExport.SheetOptions.NoteFormat.Alignment.Horizontal 
= HorizontalAlignment.Left;
        cellExport.SheetOptions.NoteFormat.Alignment.Vertical 
= VerticalAlignment.Top;
        cellExport.SheetOptions.NoteFormat.Font.Bold 
= true;
        cellExport.SheetOptions.NoteFormat.Font.Name 
= "Tahoma";
        cellExport.SheetOptions.NoteFormat.Font.Size 
= 8F;
        cellExport.SheetOptions.TitlesFormat.Font.Bold 
= true;
        cellExport.SheetOptions.TitlesFormat.Font.Name 
= "Arial";

        cellExport.DataSource 

= ExportSource.DataTable;
        cellExport.DataTable 
= dbTable;
        cellExport.FileName 
= strFileName;
        cellExport.SaveToFile();
 }

以下示例将一个DataTable输出成PDF文件:

static void ExportToPDF(DataTable dbTable, string strFileName)
{
        PDFExport pdfExport 
= new PDFExport();
        pdfExport.ActionAfterExport 
= ActionType.OpenView;
        pdfExport.DataFormats.CultureName 
= "en-US";
        pdfExport.DataFormats.Currency 
= "#,###,##0.00000";
        pdfExport.DataFormats.DateTime 
= "yyyy-M-d HH:mm";
        pdfExport.DataFormats.Float 
= "#,###,##0.00";
        pdfExport.DataFormats.Integer 
= "#,###,##0";
        pdfExport.DataFormats.Time 
= "H:mm";
        pdfExport.PDFOptions.PageOptions.Format 
= PageFormat.User;
        pdfExport.PDFOptions.PageOptions.Height 
= 11.67;
        pdfExport.PDFOptions.PageOptions.MarginBottom 
= 0.78;
        pdfExport.PDFOptions.PageOptions.MarginLeft 
= 1.17;
        pdfExport.PDFOptions.PageOptions.MarginRight 
= 0.57;
        pdfExport.PDFOptions.PageOptions.MarginTop 
= 0.78;
        pdfExport.PDFOptions.PageOptions.Width 
= 8.25;

        pdfExport.DataSource 

= ExportSource.DataTable;
        pdfExport.DataTable 
= dbTable;

        pdfExport.FileName 

= strFileName;
        pdfExport.SaveToFile();
}

以下示例将一个DataTable输出成HTML文件:

static void ExportToHTML(DataTable dbTable, string strFileName)
        {
            HTMLExport htmlExport 
= new HTMLExport();
            htmlExport.ActionAfterExport 
= ActionType.None;
            htmlExport.DataFormats.CultureName 
= "en-US";
            htmlExport.DataFormats.Currency 
= "#,###,##0.00";
            htmlExport.DataFormats.DateTime 
= "yyyy-M-d H:mm";
            htmlExport.DataFormats.Float 
= "#,###,##0.00";
            htmlExport.DataFormats.Integer 
= "#,###,##0";
            
//htmlExport.DataFormats.Time = "H:mm";
            htmlExport.HtmlStyle = HtmlStyle.MSMoney;
            htmlExport.HtmlTextOptions.Font 
= new System.Drawing.Font("Arial", 8F);

            htmlExport.DataSource 

= ExportSource.DataTable;
            htmlExport.DataTable 
= dbTable;

            htmlExport.FileName 

= strFileName;
            htmlExport.SaveToFile();
        }