导出 word2007
导入命名空间
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using System.Data;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using System.Data;
创建虚拟数据
Code
private static DataSet CreateMockDataSet()
{
DataTable table = new DataTable("sample");
DataColumn column;
DataRow row;
// Create the first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "DocID";
column.AutoIncrement = true;
column.Caption = "ID";
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
// Create the second column and add it to the column set.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "DocName";
column.AutoIncrement = false;
column.Caption = "DocName";
table.Columns.Add(column);
// Create the third column and add it to the column set.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ChapterTitle";
column.AutoIncrement = false;
column.Caption = "ChapterTitle";
table.Columns.Add(column);
// Create five DataRow objects, and add them to DataTable.
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["DocID"] = i;
row["DocName"] = "Doc " + i;
row["ChapterTitle"] = "Chapter Title " + i;
table.Rows.Add(row);
}
// Create a data set from the table and return it.
DataSet ds = new DataSet();
ds.Tables.Add(table);
return ds;
}
private static DataSet CreateMockDataSet()
{
DataTable table = new DataTable("sample");
DataColumn column;
DataRow row;
// Create the first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "DocID";
column.AutoIncrement = true;
column.Caption = "ID";
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
// Create the second column and add it to the column set.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "DocName";
column.AutoIncrement = false;
column.Caption = "DocName";
table.Columns.Add(column);
// Create the third column and add it to the column set.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ChapterTitle";
column.AutoIncrement = false;
column.Caption = "ChapterTitle";
table.Columns.Add(column);
// Create five DataRow objects, and add them to DataTable.
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["DocID"] = i;
row["DocName"] = "Doc " + i;
row["ChapterTitle"] = "Chapter Title " + i;
table.Rows.Add(row);
}
// Create a data set from the table and return it.
DataSet ds = new DataSet();
ds.Tables.Add(table);
return ds;
}
调用openXML sdk生成word2007,并输出到客户端
Code
protected void Button1_Click(object sender, EventArgs e)
{
File.Copy(Server.MapPath(@"sampledoc/createtable.docx"), Server.MapPath(@"sampledoc/generated.docx"), true);
using (WordprocessingDocument doc = WordprocessingDocument.Open(Server.MapPath(@"sampledoc/generated.docx"), true))
{
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
// Create TableProperties to specify the table border information
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 }
)
);
table.AppendChild<TableProperties>(tblProp);
// Create a header row
DocumentFormat.OpenXml.Wordprocessing.TableRow headerRow = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
table.AppendChild(headerRow);
foreach (DataColumn column in CreateMockDataSet().Tables[0].Columns)
{
// Create a cell
DocumentFormat.OpenXml.Wordprocessing.TableCell tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc.Append(new Paragraph(new Run(new Text(column.ColumnName))));
headerRow.Append(tc);
}
// Fill in the data rows
foreach (DataRow row in CreateMockDataSet().Tables[0].Rows)
{
// Create the first row
DocumentFormat.OpenXml.Wordprocessing.TableRow tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
// Create a cell
DocumentFormat.OpenXml.Wordprocessing.TableCell tc1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc1.Append(new Paragraph(new Run(new Text(row["DocID"].ToString()))));
tr.Append(tc1);
// Create the second cell in the row
DocumentFormat.OpenXml.Wordprocessing.TableCell tc2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc2.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc2.Append(new Paragraph(new Run(new Text(row["DocName"].ToString()))));
tr.Append(tc2);
// Create the third cell in the row
DocumentFormat.OpenXml.Wordprocessing.TableCell tc3 = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc3.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc3.Append(new Paragraph(new Run(new Text(row["ChapterTitle"].ToString()))));
tr.Append(tc3);
table.AppendChild(tr);
}
// Append the table to the document
doc.MainDocumentPart.Document.Body.Append(table);
// Since we changed the MainDocumentPart, we need to save the modification back to the part
doc.MainDocumentPart.Document.Save();
}
//将导出的word2007文件输出到客户端
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(@"aa.docx", System.Text.Encoding.UTF8));
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.WriteFile(Server.MapPath(@"sampledoc/generated.docx"));
Response.Flush();
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
File.Copy(Server.MapPath(@"sampledoc/createtable.docx"), Server.MapPath(@"sampledoc/generated.docx"), true);
using (WordprocessingDocument doc = WordprocessingDocument.Open(Server.MapPath(@"sampledoc/generated.docx"), true))
{
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
// Create TableProperties to specify the table border information
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 },
new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 1 }
)
);
table.AppendChild<TableProperties>(tblProp);
// Create a header row
DocumentFormat.OpenXml.Wordprocessing.TableRow headerRow = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
table.AppendChild(headerRow);
foreach (DataColumn column in CreateMockDataSet().Tables[0].Columns)
{
// Create a cell
DocumentFormat.OpenXml.Wordprocessing.TableCell tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc.Append(new Paragraph(new Run(new Text(column.ColumnName))));
headerRow.Append(tc);
}
// Fill in the data rows
foreach (DataRow row in CreateMockDataSet().Tables[0].Rows)
{
// Create the first row
DocumentFormat.OpenXml.Wordprocessing.TableRow tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
// Create a cell
DocumentFormat.OpenXml.Wordprocessing.TableCell tc1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc1.Append(new Paragraph(new Run(new Text(row["DocID"].ToString()))));
tr.Append(tc1);
// Create the second cell in the row
DocumentFormat.OpenXml.Wordprocessing.TableCell tc2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc2.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc2.Append(new Paragraph(new Run(new Text(row["DocName"].ToString()))));
tr.Append(tc2);
// Create the third cell in the row
DocumentFormat.OpenXml.Wordprocessing.TableCell tc3 = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Specify the table cell property by setting the table cell width to 300 points
tc3.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = 2400 }));
// Set the table cell content
tc3.Append(new Paragraph(new Run(new Text(row["ChapterTitle"].ToString()))));
tr.Append(tc3);
table.AppendChild(tr);
}
// Append the table to the document
doc.MainDocumentPart.Document.Body.Append(table);
// Since we changed the MainDocumentPart, we need to save the modification back to the part
doc.MainDocumentPart.Document.Save();
}
//将导出的word2007文件输出到客户端
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(@"aa.docx", System.Text.Encoding.UTF8));
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.WriteFile(Server.MapPath(@"sampledoc/generated.docx"));
Response.Flush();
Response.End();
}