问题描述
//i'm using VS 2008, dll as Microsoft.Office.Interop.Excel
//sample code..
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);//create new work book
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//CREATING ONE Rectangle shape
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 47, 24, 500, 90);
Microsoft.Office.Interop.Excel.Shape[] myShapes = new Microsoft.Office.Interop.Excel.Shape[1];
//adding textbox
myShapes[0] = xlWorkSheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 75, 60, 70, 30);
myShapes[0].TextFrame.Characters(misValue, misValue).Text = "Tracking";
//浏览器从下载选项。
//download option from browser.
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("Content-Disposition", "attachment; filename= report.xls");
//这里我使用临时文本file.but我需要不使用临时文件。
//here i'm using temporary text file.but i need without using temporary file..
string file = context.Server.MapPath("new.txt");
Byte[] myByte = File.ReadAllBytes(file);
context.Response.Clear();
context.Response.BinaryWrite(myByte);
context.Response.Flush();
context.Response.Close();
我们可以使用IPersistStream接口界面保存的MemoryStream对象?
Can we use Ipersiststream interface to save the objects in memorystream ?
http://www.eggheadcafe.com/community/csharp/2/10101323/how-to-implement-ipersiststream-interface-in-c.aspx
推荐答案
如果你有比GridView的更多的格式创建一个Excel文档可以做的兴趣可能要看一看EPPlus:
If you have an interest in creating an Excel document with more formatting than the GridView can do you may want to take a look at EPPlus:
该库能够与定制单元,格式等等的能力,创建Excel文件的。
The library is capable of creating Excel files with the ability to customize cells, formats, etc.
如果在GridView为你工作,去了,但如果不是有其他Excel选项。你应该能够将内容保存到内存流和输出内存流到浏览器。
If the GridView works for you, go for it, but if not there are other Excel options. You should be able to save the content to a memory stream and output the memory stream to the browser.
下面是我的ASP.NET MVC使用FileResult handeler的样本。的概念可以应用到的WebForms,它是所有写出到响应流。我的类有你想要的浏览器下载文件(文件名)的数据表中加载一个属性(this.dataTable)和文件名和工作簿名称(this.workBookName)
Here is a sample of a FileResult handeler that I've used with ASP.NET MVC. The concept can be applied to WebForms, all it does is write out to the response stream. My class has a property for the datatable to load (this.dataTable) and the file name you want the browser to download the file as (fileName) and the work book name (this.workBookName)
protected override void WriteFile(HttpResponseBase response)
{
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(this.workBookName);
ws.Cells["A1"].LoadFromDataTable(this.dataTable, true);
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.BinaryWrite(pck.GetAsByteArray());
}
}
这篇关于1)如何写赛过的MemoryStream .... 2)如何下载练成从浏览器中的文件....使用Micosoft.Office.Interop.Excel和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!