如何使用asp.net创建和下载excel文档 [英] how to create and download excel document using asp.net
问题描述
如何使用asp.net创建和下载excel文档?
目的是使用xml,linq或其他方式通过以下方式向客户发送excel文档浏览器。
编辑:用例
客户加载一个gridview与ajax框架)在浏览器中,gridview直接链接到一个sql数据库。
我把一个按钮'export to excel'让客户把他的这个gridview数据保存在他的电脑上,我想启动一个干净的excel下载。
这里提出的解决方案不是很干净,就像发送一个html文档,并将标题更改为excel文档等,我正在搜索一个简单的解码在codeplex现在,我会让你知道。
入门套件
首先我下载了 打开XML格式SDK 2.0 。 / p>
它附带了3个有用的工具:
C:\Program Files\打开XML格式SDK\\ \\ V2.0\tools
-
DocumentReflector.exe
wich auto
生成c#从代码中构建
电子表格。 -
OpenXmlClassesExplorer.exe
显示
Ecma规范和类
文档(使用MSDN风格
格式)。 -
OpenXmlDiff.exe
以图形方式比较
两个Open XML文件并搜索
错误。
I建议任何开始重命名 .xlsx .zip 的人,因此您可以请参阅驱动我们的电子表格的XML文件(例如我们的表格在xl\worksheets中)。
代码
免责声明:我已经从 MSDN技术文章 ; D
以下代码使用* .xlsx模板,我手动进行修改。
命名空间引用
使用System.IO;
使用System.Xml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Spreadsheet;
使用DocumentFormat.OpenXml;
//数据库对象
DataClassesDataContext db = new DataClassesDataContext();
//创建模板文件的副本。
File.Copy(@C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\livreurs.xlsx,@C:\inetpub\ wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx,true);
//打开复制的模板工作簿。
using(SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(@C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx,true))
{
//访问主工作簿部分,其中包含所有引用。
WorkbookPart workbookPart = myWorkbook.WorkbookPart;
//获取第一个工作表。
WorksheetPart worksheetPart = workbookPart.WorksheetParts.ElementAt(2);
// SheetData对象将包含所有数据。
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild< SheetData>();
//开始行指针
int index = 2;
//数据库结果
var query = from t in db.Clients select t;
//对于数据库中的每个项目,将一行添加到SheetData。
foreach(查询中的var项)
{
//单元格相关变量
string Nom = item.Nom;
// New Row
Row row = new Row();
row.RowIndex =(UInt32)index;
// New Cell
Cell cell = new Cell();
cell.DataType = CellValues.InlineString;
//列A1,2,3 ...等等
cell.CellReference =A+索引;
//创建文本对象
文本t = new Text();
t.Text = Nom;
//将文本附加到InlineString对象
InlineString inlineString = new InlineString();
inlineString.AppendChild(t);
//将InlineString附加到单元格
cell.AppendChild(inlineString);
//将单元格追加到行
row.AppendChild(cell);
//将行附加到SheetData
sheetData.AppendChild(row);
//增加行指针
index ++;
}
//保存
工作表Part.Worksheet.Save();
}
我没有完成,我的第二份工作是自动下载电子表格修改后。
最后,我将用户重定向到我生成的spredsheet(从我的aspx)
context.Response.Redirect(Oxml-tpl / generated.xlsx);
How to create and download excel document using asp.net ?
The purpose is to use xml, linq or whatever to send an excel document to a customer via a browser.
Edit : Use case
The customer load a gridview ( made with ajax framework ) in a browser, the gridview is directly linked to an sql database. I put a button 'export to excel' to let customer save this gridview data on his computer ansd i would like to launch a clean download of an excel.
The solutions proposed here are not clean, like send an html document and change the header to excel document etc, i'm searching a simple solution on codeplex right now, i will let you know.
Starter kit
First i have downloaded the Open XML Format SDK 2.0.
It comes with 3 useful tools in :
C:\Program Files\Open XML Format SDK\V2.0\tools
DocumentReflector.exe
wich auto generate the c# to build a spreadsheet from the code.OpenXmlClassesExplorer.exe
display Ecma specification and the class documentation (using an MSDN style format).OpenXmlDiff.exe
graphically compare two Open XML files and search for errors.
I suggest anyone who begin to rename .xlsx to .zip, so you can see the XML files who drive our spreadsheet ( for the example our sheets are in "xl\worksheets" ).
The code
Disclaimer : I have stolen all the code from an MSDN technical article ;D
The following code use an *.xlsx template i made manually to be able to modify it.
Namespaces references
using System.IO;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
// Database object
DataClassesDataContext db = new DataClassesDataContext();
// Make a copy of the template file.
File.Copy(@"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\livreurs.xlsx", @"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx", true);
// Open the copied template workbook.
using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(@"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx", true))
{
// Access the main Workbook part, which contains all references.
WorkbookPart workbookPart = myWorkbook.WorkbookPart;
// Get the first worksheet.
WorksheetPart worksheetPart = workbookPart.WorksheetParts.ElementAt(2);
// The SheetData object will contain all the data.
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
// Begining Row pointer
int index = 2;
// Database results
var query = from t in db.Clients select t;
// For each item in the database, add a Row to SheetData.
foreach (var item in query)
{
// Cell related variable
string Nom = item.Nom;
// New Row
Row row = new Row();
row.RowIndex = (UInt32)index;
// New Cell
Cell cell = new Cell();
cell.DataType = CellValues.InlineString;
// Column A1, 2, 3 ... and so on
cell.CellReference = "A"+index;
// Create Text object
Text t = new Text();
t.Text = Nom;
// Append Text to InlineString object
InlineString inlineString = new InlineString();
inlineString.AppendChild(t);
// Append InlineString to Cell
cell.AppendChild(inlineString);
// Append Cell to Row
row.AppendChild(cell);
// Append Row to SheetData
sheetData.AppendChild(row);
// increase row pointer
index++;
}
// save
worksheetPart.Worksheet.Save();
}
I havent finished yet, my second job is to auto download the spreadsheet after modification.
Finally, i redirect the user to my generated spredsheet (from my aspx)
context.Response.Redirect("Oxml-tpl/generated.xlsx");
这篇关于如何使用asp.net创建和下载excel文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!