如何将日期集写入2007年的新Excel文件中,而不是2003年 [英] How to write a dateset into a new excel file of 2007 but not 2003

查看:75
本文介绍了如何将日期集写入2007年的新Excel文件中,而不是2003年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

谁能告诉我如何直接从数据集中生成新的Excel 2007文件.
我使用了一段代码,但是它以旧的2003格式生成了excel文件.

我的代码是:

Hi all,

Can anyone tell me how to generate a new excel 2007 file from a dataset directly.
I have used a piece of code, but it its generating the excel file in old 2003 format.

My Code is:

public void ExportDataSetToExcel(DataSet ds, string filename)
{
    HttpResponse response = HttpContext.Current.Response;

    response.Clear();
    response.Charset = "";

    response.ContentType = "application/vnd.ms-excel";
    response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    // instantiate a datagrid
    DataGrid dg = new DataGrid();
    dg.DataSource = ds.Tables[0];

    dg.DataBind();
    dg.RenderControl(htw);
    response.Write(sw.ToString());
    response.End();
}


在此先感谢...


Thanks in advance...

推荐答案

老兄,

为此,您需要安装2007 Microsoft Tool,然后它将代码将数据集转换为excel


在您的页面指令中,放入EnableEventValidation=false

hi dude,

For this you need to install 2007 Microsoft Tool then this will code will convert the data set into excel


In your page directive put EnableEventValidation=false

public void ExportGridExel()
            {
                  string attachment = "attachment; filename=EstablishmentAllotmentDetail.xls";
                  Response.ClearContent();
                  Response.AddHeader("content-disposition", attachment);
                  Response.ContentType = "application/ms-excel";
                  StringWriter sw = new StringWriter();
                  HtmlTextWriter htw = new HtmlTextWriter(sw);
                  HtmlForm frm = new HtmlForm();
                  if (trAllotmentDetail.Visible == true)
                  {
                        pnlGrid.Parent.Controls.Add(frm);
                        frm.Controls.Add(pnlGrid);
                  }
                  if (trAllotmentDetailDivision.Visible == true)
                  {
                        pnlDivision.Parent.Controls.Add(frm);
                        frm.Controls.Add(pnlDivision);
                  }
                  frm.Attributes["runat"] = "server";
                  frm.RenderControl(htw);
                  Response.Write(sw.ToString());
                  Response.End();
            }

public override void VerifyRenderingInServerForm(Control control)
      {
            /* Verifies that the control is rendered */
   }


通过以下链接,我们可以获得将数据集写入excel 2007文件的代码.

www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm
By following the below link we can get the code of writing dataset into a excel 2007 file.

www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm


这篇关于如何将日期集写入2007年的新Excel文件中,而不是2003年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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