数据导出到Excel,Word和PDF格式使用 [英] Export data into Excel, Word and PDF with Formatting

查看:117
本文介绍了数据导出到Excel,Word和PDF格式使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想数据表或数据集的数据导出像页眉页脚,字体大小,行颜色的颜色在Word,Excel和PDF格式格式化。这可能吗?

如果是的话怎么样?请healp我。
我的code是如下图所示。

 公共无效ExportToExcel(DataTable的DT)
{
        con.Open();
        字符串SQL =选择测试*;
        CMD =新的SqlCommand(SQL,CON);
        DT =新的DataTable();
        DA =新SqlDataAdapter的(CMD);
        da.Fill(DT);
        GridView1.DataSource = DT;
        GridView1.DataBind();
        cmd.Dispose();        字符串文件名=DownloadTest.xls;
        System.IO.StringWriter TW =新System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter HW =新System.Web.UI.HtmlTextWriter(TW);
        DataGrid中dgGrid =新的DataGrid();
        dgGrid.DataSource = DT;
        dgGrid.DataBind();
        DataSet的DS =新的DataSet();
        DS = dt.Clone;        dgGrid.RenderControl(HW);        Response.ContentType =应用程序/ vnd.ms-EXCEL;
        Response.AppendHeader(内容处置,附件;文件名=+文件名+);
        this.EnableViewState = FALSE;
        的Response.Write(tw.ToString());
        到Response.End();
        con.Close();
}


解决方案

有关PDF: -

我认为你应该使用'iTextSharp的。你可以通过这个链接
HTTP:// WWW。 codeproject.com /用品/ 81118 / iTextSharp的-助手级

你可以从这里下载
其文件(.dll)
http://sourceforge.net/projects/itextsharp/

有关Excel和Word可以使用GridView控件来导出它Excel和Word的颜色和字体。

为Excel你可以通过这个链接

I want to export data of DataTable or DataSet with formating like Color of Header-Footer, Font Size, Row Color in Word, Excel and PDF format. Is it possible?

If yes then how? Please healp me. My code is as below.

public void ExportToExcel(DataTable dt)
{   
        con.Open();
        string sql = "select *from test";
        cmd = new SqlCommand(sql, con);
        dt = new DataTable();
        da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        cmd.Dispose();

        string filename = "DownloadTest.xls";
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        DataGrid dgGrid = new DataGrid();
        dgGrid.DataSource = dt;
        dgGrid.DataBind();
        DataSet ds = new DataSet();
        ds = dt.Clone;

        dgGrid.RenderControl(hw);

        Response.ContentType = "application/vnd.ms-excel";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
        this.EnableViewState = false;
        Response.Write(tw.ToString());
        Response.End();


        con.Close();
}

解决方案

for pdf :-

I think you should use 'itextSharp'. You can go through this link
http://www.codeproject.com/Articles/81118/ITextSharp-Helper-Class
You can download it (.dll) from here
http://sourceforge.net/projects/itextsharp/

For excel and word you can use gridview to export it to excel and word with color and fonts.
for excel you can go through this link

这篇关于数据导出到Excel,Word和PDF格式使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆