我们让用户在我们的网站上创建临时查询。我们希望让用户选择他们的标准,然后点击提交并将结果自动传输到Excel。我有应用程序填充DataTable,然后使用数据表创建制表符分隔的字符串。问题在于如何达到卓越。从ASP.NET网站发送查询结果到Excel
什么是流数据到Excel的最佳方式?最好是,我们不必让用户点击提交按钮后关闭一个空的窗口。
我们让用户在我们的网站上创建临时查询。我们希望让用户选择他们的标准,然后点击提交并将结果自动传输到Excel。我有应用程序填充DataTable,然后使用数据表创建制表符分隔的字符串。问题在于如何达到卓越。从ASP.NET网站发送查询结果到Excel
什么是流数据到Excel的最佳方式?最好是,我们不必让用户点击提交按钮后关闭一个空的窗口。
将页面的文件类型更改为excel,并且只将HTML表单构建到页面所需的HTML。从here
//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);
代码我建议使用filehandler (.ashx)唯一的问题是创建从DataTable中的Excel文件。有很多第三方产品可以为您做到这一点(例如Infragistics提供了一个可以做到这一点的组件)。
我强烈建议的一件事是在您的服务器上使用Excel interop ...它非常重量级,不受支持。
如果您创建的页面只是包含结果的表格,并将页面的内容类型设置为“application/vnd.ms-excel”,则输出将显示在Excel中。
Response.ContentType = "application/vnd.ms-excel";
如果要强制保存,你会做类似如下:
Response.AddHeader("Content-Disposition", "attachment; filename=somefilename.xls");
一旦你有你的数据集,你可以将它转换为对象[,]并将其插入到一个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;
}
但是不要在服务器上这样做(自动化Excel)。 http://support.microsoft.com/kb/257757 – Cheeso 2009-05-29 16:42:57
这是在客户端完成的。 – 2009-05-29 21:51:09
我会使用.xls文件扩展名的处理程序和一个空闲组件将DataTable转换为本机xls格式。来自此网站http://www.csvreader.com/的组件更多地表明了该网址的含义。最新版本的excel会抱怨HTML格式的XLS文件。同时请记住所返回数据的大小。你的web服务器应该对这个扩展使用压缩,你的代码应该检查返回的行数是否大于excel可以在一个工作表中显示的数量;可能需要多张纸。 http://www.mrexcel.com/archive2/23600/26869.htm
我得到了一个utils函数,它已经这样做了。一旦你把它放到一个数据表,你可以使用
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();
}
请使用此代码来解决您的problem.This代码转换Excel表单文本format.Hope响应导出,这将解决您的问题
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);
请注意,这不会创建一个正常的excel文件,就像您打开excel并手动创建一个一样...这是一个html文件。 – 2008-09-29 14:56:03