我想将工作表转换为内存流,以便我可以在浏览器中下载excel。
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// Create empty workbook
excel.Workbooks.Add();
// Create Worksheet from active sheet
Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet;
try
{
// ------------------------------------------------
// Creation of header cells
// ------------------------------------------------
workSheet.Cells[1, "A"] = "GroupId";
workSheet.Cells[1, "B"] = "Time";
// ------------------------------------------------
// Populate sheet with some real data from "cars" list
// ------------------------------------------------
int row = 2; // start row (in row 1 are header cells)
foreach (var item in Result)
{
workSheet.Cells[row, "A"] = item.MasterID;
workSheet.Cells[row, "B"] = item.LocalTime;
row++;
}
我知道转换成内存流但是在这里
using (MemoryStream exportData = new MemoryStream())
{
//workSheet.Write(exportData); //Worksheet doesn't have this Write method as compared to workbook.
Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
Response.Charset = Encoding.GetEncoding("ISO-8859-1").EncodingName;
Response.ContentType = "application/vnd.ms-excel"; //xls
// For xlsx, use: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Response.AddHeader("content-disposition", String.Format("attachment; filename={0}.xls", "yourFilename"));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
如何将我的工作表转换为内存流?
答案 0 :(得分:0)
找不到Microsoft.Office.Interop.Excel的解决方案。除了文件应首先保存在计算机上,而不是流保存为红色。
Spire.xsl有一个方法
workbook.SaveToStream(memoryStream,FileFormat.Version2016);
注意:spire.xsl免费版本有限制。