ASP.NET2.0 生成Word 2007并下载方案
1.开发用途:用于将页面数据或者后台数据生成word2007格式,并提供下载。
2.开发环境:vs2008 + office2007 + DocumentFormat.OpenXml sdk(我用的版本是:2.0.4330.0,具体组件可在微软官方下载)。
3.主要代码如下:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
/// <summary>
/// 生成2007文档
/// </summary>
/// <param name="docName"></param>
public void CreateDocumentFile(string docName)
{
// Create a Wordprocessing document.
string temp = Server.MapPath(@"..\Template\个人履历.docx");
if (File.Exists(temp)) File.Delete(temp);
File.Copy(docName, temp);
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(temp, true))
{
DocumentFormat.OpenXml.Wordprocessing.Table table = CreateMainTable(12, 4, true,myDoc);
myDoc.MainDocumentPart.Document.Body.Append(table);
myDoc.MainDocumentPart.Document.Save();
}
ResponseFile(temp);
}
/// <summary>
/// 生成页面主表格
/// </summary>
/// <param name="rowCount"></param>
/// <param name="ColumnCount"></param>
/// <param name="HaveBorder"></param>
/// <param name="wd"></param>
/// <returns></returns>
protected DocumentFormat.OpenXml.Wordprocessing.Table CreateMainTable(int rowCount, int ColumnCount, bool HaveBorder,WordprocessingDocument wd)
{
//创建新表格
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
//设置表格边线
DocumentFormat.OpenXml.Wordprocessing.TableProperties tblPr = TpHaveBorder(true);
table.Append(tblPr);
DocumentFormat.OpenXml.Wordprocessing.TableRow tr =new DocumentFormat.OpenXml.Wordprocessing.TableRow();
DocumentFormat.OpenXml.Wordprocessing.TableCell tc=new DocumentFormat.OpenXml.Wordprocessing.TableCell();
//first row - title
//主要资料录入
DocumentFormat.OpenXml.Wordprocessing.Table ta = CreateSubTable(1, 2,wd);
tc.Append(ta);
//
TableCellProperties tcp = new TableCellProperties();
GridSpan gridSpan = new GridSpan();
gridSpan.Val = 11;
tcp.Append(gridSpan);
tc.Append(tcp);
DocumentFormat.OpenXml.Wordprocessing.Text x = new DocumentFormat.OpenXml.Wordprocessing.Text("");
Run run_paragraph = new Run();
run_paragraph.Append(x);
DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
paragraph.Append(run_paragraph);
tc.Append(paragraph); tr.Append(tc); table.Append(tr);
IList<LINQDB.HRDB.EmpFile_Education> edu;
IList<LINQDB.HRDB.EmpFile_Family> fam;
IList<LINQDB.HRDB.EmpFile_Working> work;
IList<LINQDB.HRDB.EmpFile_Emergency> emer;
using (var db = new LINQDB.HRDB.HRDBDataContext())
{
fam = db.EmpFile_Family.Where(p => p.EmpID == PageArg.Id1).ToList();
edu = db.EmpFile_Education.Where(p => p.EmpID == PageArg.Id1).ToList();
emer = db.EmpFile_Emergency.Where(p => p.EmpID == PageArg.Id1).ToList();
work = db.EmpFile_Working.Where(p => p.EmpID == PageArg.Id1).ToList();
}
table = CreateFamilyDoc(table, fam); table = CreateEduDoc(table, edu); table = CreateEmerDoc(table,emer);
table = CreateWorkDoc(table, work);
return table;
}
/// <summary>
/// 创建子表格
/// </summary>
/// <param name="rowCount"></param>
/// <param name="ColumnCount"></param>
/// <returns></returns>
protected DocumentFormat.OpenXml.Wordprocessing.Table CreateSubTable(int rowCount, int ColumnCount,WordprocessingDocument wd)
{
&
补充:Web开发 , ASP.Net ,