5

我们让用户在我们的网站上创建临时查询。我们希望用户选择他们的标准,然后单击提交并将结果自动传输到 Excel。我让应用程序填充数据表,然后使用数据表创建制表符分隔的字符串。问题是让它变得优秀。

将数据流式传输到 Excel 的最佳方式是什么?最好,我们不必让用户在单击提交按钮后关闭一个空窗口。

4

7 回答 7

10

将页面的文件类型更改为 excel,并且仅将构建表格所需的 HTML 流式传输到页面。来自这里的代码

//for demo purpose, lets create a small datatable & populate it with dummy data
System.Data.DataTable workTable = new System.Data.DataTable();

//The tablename specified here will be set as the worksheet name of the generated Excel file. 
workTable.TableName = "Customers";
workTable.Columns.Add("Id");
workTable.Columns.Add("Name");
System.Data.DataRow workRow;

for (int i = 0; i <= 9; i++)
{
workRow = workTable.NewRow();
workRow[0] = i;
workRow[1] = "CustName" + i.ToString();
workTable.Rows.Add(workRow);
}

//...and lets put DataTable2ExcelString to work
string strBody = DataTable2ExcelString(workTable);

Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
Response.AppendHeader("Content-disposition", "attachment; filename=my.xls");
Response.Write(strBody);
于 2008-09-29T14:37:21.983 回答
1

如果您创建的页面只是包含结果的表格并将页面的内容类型设置为“application/vnd.ms-excel”,则输出将在 Excel 中。

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

如果要强制保存,可以执行以下操作:

Response.AddHeader("Content-Disposition", "attachment; filename=somefilename.xls");
于 2008-09-29T14:47:27.293 回答
1

我已经有了一个 utils 函数。将其放入数据表后,您可以使用 Response 将其导出

        public static void DataTabletoXLS(DataTable DT, string fileName)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "utf-16";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        string tab = "";
        foreach (DataColumn dc in DT.Columns)
        {
            HttpContext.Current.Response.Write(tab + dc.ColumnName.Replace("\n", "").Replace("\t", ""));
            tab = "\t";
        }
        HttpContext.Current.Response.Write("\n");

        int i;
        foreach (DataRow dr in DT.Rows)
        {
            tab = "";
            for (i = 0; i < DT.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write(tab + dr[i].ToString().Replace("\n", "").Replace("\t", ""));
                tab = "\t";
            }
            HttpContext.Current.Response.Write("\n");
        }
        HttpContext.Current.Response.End();
               }
于 2008-09-29T19:56:44.523 回答
0

我建议使用文件处理程序 (.ashx)唯一的问题是从 DataTable 创建 excel 文件。有很多第三方产品可以为您执行此操作(例如 Infragistics 提供了一个可以执行此操作的组件)。

我强烈建议反对的一件事是在您的服务器上使用 Excel 互操作......它非常重量级并且不受支持。

于 2008-09-29T14:39:20.963 回答
0

拥有数据集后,您可以将其转换为 object[,] 并将其插入 Excel 文档。然后,您可以将文档保存到磁盘并将其流式传输给用户。

        //write the column headers
        for (int cIndex = 1; cIndex < 1 + columns; cIndex++)
            sheet.Cells.set_Item(4, cIndex, data.Columns[cIndex - 1].Caption);
        if (rows > 0)
        {

            //select the range where the data will be pasted
            Range r = sheet.get_Range(sheet.Cells[5, 1], sheet.Cells[5 + (rows - 1), columns]);

            //Convert the datatable to an object array
            object[,] workingValues = new object[rows, columns];

            for (int rIndex = 0; rIndex < rows; rIndex++)
                for (int cIndex = 0; cIndex < columns; cIndex++)
                    workingValues[rIndex, cIndex] = data.Rows[rIndex][cIndex].ToString();

            r.Value2 = workingValues;
         }
于 2008-09-29T15:06:07.110 回答
0

我将使用 .xls 文件扩展名的处理程序和一个免费组件将 DataTable 转换为本机 xls 格式。该站点http://www.csvreader.com/中的组件比 URL 所暗示的更多。最新版本的 excel 将抱怨 HTML 格式的 XLS 文件。还要记住要返回的数据的大小。您的 Web 服务器应该对此扩展使用压缩,并且您的代码应该检查返回的行数是否大于 Excel 在一个工作表中可以显示的行数;可能需要多张纸。http://www.mrexcel.com/archive2/23600/26869.htm

于 2008-09-29T15:24:48.843 回答
0

请使用此代码解决您的问题。此代码将 excel 表转换为文本格式。希望这能解决您的问题

    grdSrcRequestExport.RenderControl(oHtmlTextWriter);
    string s = "";
    s=oStringWriter.ToString().Replace("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">", "");
    s="<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=us-ascii\"><meta name=ProgId content=Excel.Sheet><meta name=Generator content=\"Microsoft Excel 11\"><table x:str border=0 cellpadding=0 cellspacing=0 width=560 style='border-collapse: collapse;table-layout:fixed;width:420pt'>"+s.ToString()+"</table></body></html>";
    //Byte[] bContent = System.Text.Encoding.GetEncoding("utf-8").GetBytes();
    Response.Write(s);
于 2009-06-29T09:51:59.053 回答