【问题标题】:ASP.NET Excel ExportASP.NET Excel 导出
【发布时间】:2012-08-14 00:54:04
【问题描述】:

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

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

例如,它当前写出 第 1 行:RowTitle 值 1 第 2 行:值 2 第 3 行:值 3

我想要的是将所有内容写在一行中。 第 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();

提前致谢。

【问题讨论】:

    标签: c# asp.net excel


    【解决方案1】:

    希望这段代码对你有所帮助,

        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("\"", "'") + "\"" + ",";
        }
    

    【讨论】:

    • 谢谢 - 我明天会调查这个问题,如果它可以解决问题,请告诉你!
    【解决方案2】:

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

    像这样的东西。 OWC

    【讨论】:

      【解决方案3】:

      当您说“导出”时,我假设您的意思是您希望能够创建一个电子表格供用户下载。如果这就是你的意思,那么你可能想看看“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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多