0

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

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

例如,它当前写出第 1 行:RowTitle Value1 第 2 行:Value2 第 3 行:Value3

我想要的是将所有内容写在一行中。第 1 行:行标题 值 1 值 2 值 3 行 2:

这可能吗 - 有没有人有任何指示?

我目前的代码如下:

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();

提前致谢。

4

3 回答 3

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("\"", "'") + "\"" + ",";
    }
于 2012-08-14T05:04:34.140 回答
0

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

像这样的东西。OWC

于 2012-08-14T05:41:46.937 回答
0

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

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

于 2012-08-14T13:48:45.237 回答