ASP.NET Excel导出

时间:2012-08-14 00:54:05

标签: c# asp.net excel

我正在寻找一种导出Excel电子表格的方法,使每行的单元格中有多个值。

我目前正在使用HtmlTextWriter类将Html写入Excel内容类型格式,但它会为相应行中的每个值创建一个新的Cell。

例如,它目前正在写出来 第1行:RowTitle Value1 第2行:Value2 第3行:Value3

我想要的是将所有内容写入一行。 第1行:RowTitle Value1                               值2                               值3 ROW2:

这可能 - 有人有任何指针吗?

我目前的代码如下:

Response.Buffer = true;
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.Charset = "";
                    Response.AddHeader("content-disposition", "filename=Compare" + fileName + ".xls");
                    this.EnableViewState = false;
                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                    this.ClearControls(tblComparison);
                    tblComparison.RenderControl(oHtmlTextWriter);

                    // Embed style rules here so that when opening the file in Excel separately, the styling will stay in place
                    // and not be dependent on an external css file.
                    Response.Write(@"
        <head>
        <title>Compare</title>
        <style>
        #tblComparison { empty-cells:show;border-collapse:collapse;border:none; }
        #tblComparison tr td { vertical-align:top;text-align:center;padding:0px 5px 2px 8px;border-right:1px solid #094A8C;border-bottom:1px dotted #6699FF;font-size:.7em }
        #tblComparison tr td.header { text-align:right;font-weight:bold }
        #tblComparison tr td.header span.note { text-align:right;font-weight:normal;color:#094A8C; }
        #tblComparison tr td div.itrs { display:none }
        #tblComparison tr td textarea { width:20em }
        #tblComparison tr td textarea.objective { height:5em;font-size:.9em }
        #tblComparison tr td select { width:200px;font-size:1em }    
        #tblComparison tr td textarea.comments { height:10em;font-size:.9em }
        #tblComparison tr td textarea.multiline { height:10em;font-size:.9em }
        #tblComparison tr td.requested textarea.comments,
        #tblComparison tr td.requested textarea.objective { background-color:#F2F7FB;border:1px solid gray }
        #tblComparison tr td.requested input { font-size:1em;background-color:#F2F7FB;border:1px solid gray }
        #tblComparison tr .existRequested { background-color:#52697B; color:White;}
        div { font-size: 1em }
        </style>
        </head>
");
                    Response.Write(oStringWriter.ToString());
                    Response.End();

提前致谢。

3 个答案:

答案 0 :(得分:0)

希望此代码能为您提供帮助,

    Response.ContentType = "text/csv";
    Response.Cache.SetNoStore();
    Response.Buffer = true;
    Response.BufferOutput = true;
    Response.Charset = "UTF-8";
    Response.ContentEncoding = System.Text.Encoding.Unicode;
    Response.Write("Company, City, Country");
    //IF you want each item in each cell of a row
    byte[] buffer = System.Text.Encoding.Unicode.GetBytes(Separater(Name) + Separater(City) +Separater(Country) +  "\r\n")
    Response.OutputStream.Write(buffer, 0, buffer.Length);
    Response.OutputStream.Flush();


    private string Separater(string text)
    {
        if (text == null)
        {
            return "\"\",";
        }
        return "\"" + text.Replace("\"", "'") + "\"" + ",";
    }

答案 1 :(得分:0)

如果要从集合或数据集中获取表的数据,可以使用电子表格类动态创建excel,并根据需要从代码中对其进行格式化。

有些事情是这样的。 OWC

答案 2 :(得分:0)

当您说“导出”时,我认为您的意思是您希望能够创建供用户下载的speadsheet。如果那就是你的意思那么你可能想看看“CarlogAg Excel Xml Writer” - http://www.carlosag.net/Tools/ExcelXmlWriter/

如果您的意思是您希望能够在包含Excel电子表格中的数据的页面上创建网格,那么您可能需要考虑使用“Excel Data Reader”读取数据 - http://exceldatareader.codeplex.com/。这将为您提供一个DataTable,然后您可以将ASP.Net绑定到它,就像这样...... http://www.ezineasp.net/post/ASP-Net-Bind-GridView-to-DataTable.aspx