使用数据表在Excel工作表中导出 [英] exporting in excel sheet using datatable

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

问题描述

专家,

我正在使用SQL作为数据库在asp.net.net中开发网站.
我正在将数据表结果导出到Excel工作表中.

我正在从2个表中获取结果-一个在 inquiry 中,另一个是 inquiry_event_assigned

查询表包含有关查询的详细信息.
在查询表中有多个事件选择选项.
这些事件存储在 inquiry_event_assigned 表中.
该表包含(inquiry_event_id,inquiry_id,event_id).

我正在使用以下方法生成excel

Hi Expert,

I am developing website in asp.net.with my sql as database.
I am exporting datatable results in excel sheet.

I am fetching results from 2 table - one in inquiry and other is inquiry_event_assigned

inquiry table contains details information regarding inquiry.
In inquiry form there is multiple event selection option.
These events are stored in inquiry_event_assigned table.
This table contains (inquiry_event_id,inquiry_id,event_id) .

I am using following method to generate excel

protected void ExportToExcel(DataTable dt)
    {
        try{
        if (dt.Rows.Count > 0)
        {

            string filename = "Inquiry Reports.xls";

            string excelHeader = "Inquiry Report";

            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.BorderStyle = BorderStyle.None;

            dgGrid.DataBind();

            // Report Header

            hw.WriteLine("<b><u><font size=’3′> " + excelHeader + " </font></u></b>");

            //Get the HTML for the control.

            dgGrid.RenderControl(hw);

            //Write the HTML back to the browser.

            //Response.ContentType = "application/vnd.ms-excel";

            Response.ContentType = "application/vnd.ms-excel";

            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");

            this.EnableViewState = false;

            Response.Write(tw.ToString());

            Response.End();
            }
           }
            catch(Exception ex1)
            {
            }
    

        }



它将所有结果一张一张地导出到excel工作表中,这意味着如果与多个事件相关联的细微查询一次又一次导出.

如果有多个以上的事件与问号相关,则它们必须位于一个逗号分隔的单个单元格中.

喜欢.



It export all the results in excel sheet one by one,means if perticuler inquiry associated with multiple events exports one after another.

If there is more than one events associted with perticuler inquiry they must be in single cell with comma separated.

like.

Build Expo Kenya,Auto Afrika	Telemarketing	afsr@gest.com	Automotive	India	12/25/2011 0:00	In-Progress



在此先感谢



Thanks in advance

推荐答案

检查以下链接

将数据表转换为Excel [ http://social.msdn.microsoft.com/论坛/en-MY/csharpgeneral/thread/5486f0dd-c168-4cea-bb99-03c92254c8a1 [ http://www.daniweb.com/software-development/csharp/threads/213015 [ ^ ]
将数据表导出到与MS-Office和Open-Office兼容的XLS/CSV [
Check following link

DataTable to Excel[^]
http://social.msdn.microsoft.com/Forums/en-MY/csharpgeneral/thread/5486f0dd-c168-4cea-bb99-03c92254c8a1[^]
http://www.daniweb.com/software-development/csharp/threads/213015[^]
Export datatable to XLS/CSV compatible with MS-Office and Open-Office[^]

Mark as answer if it is helpful to you.


尝试一下
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


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

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