c#(.net) 导出 word表格

时间:2021-01-04 12:11:30

做了差不多一周的导出Word,现在把代码贴出来   :

ExportWord.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO; /// <summary>
///DaoChuWord 的摘要说明
/// </summary>
public class ExportWord
{
public ExportWord()
{
//
//TODO: 在此处添加构造函数逻辑
//
} public DataTable dat = null;
public void ProcessRequest(HttpContext context, string id,string name)
{
context.Response.ContentType = "text/plain";
GetDataTable(id);
CreateWord(name);
//此处为了批量操作,把下面这个注掉
//context.Response.End(); } DataTable GetDataTable(string id)
{
string sql = " select * from hr_person where id =" + id;
dat = ZWL.DBUtility.MyDBHelp.GetDataTable(sql);
if (dat != null)
{
return dat;
}
throw new NoNullAllowedException();
} public string CreateWord(string name)
{
string uid = dat.Rows[]["id"].ToString();
string message = ""; Object Nothing = System.Reflection.Missing.Value; object filename = name; //文件保存路径
//创建Word文档
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordApp.Selection.PageSetup.LeftMargin = 50f;
WordApp.Selection.PageSetup.RightMargin = 50f;
WordApp.Selection.PageSetup.PageWidth = 650f; //页面宽度 WordDoc.ActiveWindow.Selection.Font.Bold = ; WordApp.Selection.ParagraphFormat.LineSpacing = 13f;//设置文档的行间距
//移动焦点并换行
object count = ;
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
WordApp.Selection.ParagraphFormat.LineSpacing = 18f; WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.TypeParagraph();//插入段落
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
WordApp.Selection.Font.Size = 18f; object missing = System.Reflection.Missing.Value;
object count2 = ;
object WdLine2 = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
WordApp.Selection.Text = "应 聘 报 名 表";
WordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//单倍行距 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.TypeParagraph();//插入段落 WordApp.Selection.Font.Size = 10.5f;
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack; //文档中创建表格
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, , , ref Nothing, ref Nothing);
//设置表格样式
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 //设置表格框
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[].Width = 25f;
newTable.Columns[].Width = 30f;
newTable.Columns[].Width = 25f;
newTable.Columns[].Width = 45f;
newTable.Columns[].Width = 25f;
newTable.Columns[].Width = 25f;
newTable.Columns[].Width = 35f;
newTable.Columns[].Width = 40f;
newTable.Columns[].Width = 35f;
newTable.Columns[].Width = 15f;
newTable.Columns[].Width = 45f;
newTable.Columns[].Width = 30f;
newTable.Columns[].Width = 40f;
newTable.Columns[].Width = 50f;
newTable.Columns[].Width = 90f; //填充表格内容
for (int k = ; k < ; k++)
{
newTable.Rows[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
}
for (int k = ; k < ; k++)
{
newTable.Columns[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
}
//垂直居中
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object countjz = ;
WordApp.Selection.MoveEnd(ref unit, ref countjz);
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 //填充表格内容 第一行
newTable.Cell(, ).Range.Text = "姓名";
//合并单元格
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["name"].ToString(); newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "性 别";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["sex"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "出 生日 期";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (dat.Rows[]["createdate"].ToString().Equals(""))
{
newTable.Cell(, ).Range.Text = "";
}
else
{
newTable.Cell(, ).Range.Text = DateTime.Parse(dat.Rows[]["createdate"].ToString()).ToString("yyyy-MM-dd");
}
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "民 族";
newTable.Cell(, ).Range.Text = dat.Rows[]["minzu"].ToString(); newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Select();//选中一行 string FileName = "E:\\OA\\new\\web2\\upload\\" + dat.Rows[]["touxiang"].ToString();
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes[].Width = 72f;//图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[].Height = 90f;//图片高
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; // 填充表格内容 第二行
newTable.Cell(, ).Range.Text = "籍 贯";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["jiguan"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "出生地";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["chushengdi"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "户 口所在地";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["hukouaddress"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "身 高";
newTable.Cell(, ).Range.Text = dat.Rows[]["shengao"].ToString(); //填充表格内容 第三行
newTable.Cell(, ).Range.Text = "政治面貌";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["zhengzhi"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "入 党时 间";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (dat.Rows[]["rudangdate"].ToString().Equals(""))
{
newTable.Cell(, ).Range.Text = "";
}
else
{
newTable.Cell(, ).Range.Text = DateTime.Parse(dat.Rows[]["rudangdate"].ToString()).ToString("yyyy-MM-dd");
}
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "参加工作时间";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (dat.Rows[]["canjiaworktime"].ToString().Equals(""))
{
newTable.Cell(, ).Range.Text = "";
}
else
{
newTable.Cell(, ).Range.Text = DateTime.Parse(dat.Rows[]["canjiaworktime"].ToString()).ToString("yyyy-MM-dd");
}
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "婚姻状况";
newTable.Cell(, ).Range.Text = dat.Rows[]["hunyin"].ToString(); //填充表格内容 第四行
newTable.Cell(, ).Range.Text = "移动电话";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["phone"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "其他联系方式";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["qitalianxi"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "E-mail";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["email"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "档 案所在地";
newTable.Cell(, ).Range.Text = dat.Rows[]["danganaddress"].ToString(); //填充表格内容 第五行
newTable.Cell(, ).Range.Text = " 身份证 号 码";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["zhengjiannum"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "是否曾投递简历到XXXXXX:(如果是,请注明投递时间)";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["ifcengtoudi"].ToString(); //填充表格内容 第六行
newTable.Cell(, ).Range.Text = "家庭住址";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["tongxunaddress"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "驾龄";
newTable.Cell(, ).Range.Text = dat.Rows[]["jialing"].ToString();
newTable.Cell(, ).Range.Text = "外语种类及熟练程度";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["waiyu"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "微机操作能力";
newTable.Cell(, ).Range.Text = dat.Rows[]["weiji"].ToString(); //填充表格内容 第七行
newTable.Cell(, ).Range.Text = "收到录用通知后可到岗时间";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (dat.Rows[]["daogangdate"].ToString().Equals("三天内"))
{
newTable.Cell(, ).Range.Text = " □三天内 √一周内 □两周内 □一个月内 "; }
if (dat.Rows[]["daogangdate"].ToString().Equals("一周内"))
{
newTable.Cell(, ).Range.Text = " □三天内 √一周内 □两周内 □一个月内 "; }
if (dat.Rows[]["daogangdate"].ToString().Equals("两周内"))
{
newTable.Cell(, ).Range.Text = " □三天内 □一周内 √两周内 □一个月内 "; }
if (dat.Rows[]["daogangdate"].ToString().Equals("一个月内"))
{
newTable.Cell(, ).Range.Text = " □三天内 □一周内 □两周内 √一个月内 "; }
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
//填充表格内容 第八行
newTable.Cell(, ).Range.Text = "应聘信息来 源";
newTable.Cell(, ).Merge(newTable.Cell(, )); if (dat.Rows[]["xinxilaiyuan"].ToString().Equals("华商报无忧招聘"))
{
newTable.Cell(, ).Range.Text = "√华商报无忧招聘 □中华英才网 □智联招聘 □其他( )";
}
if (dat.Rows[]["xinxilaiyuan"].ToString().Equals("中华英才网"))
{
newTable.Cell(, ).Range.Text = "□华商报无忧招聘 √中华英才网 □智联招聘 □其他( )";
}
if (dat.Rows[]["xinxilaiyuan"].ToString().Equals("智联招聘"))
{
newTable.Cell(, ).Range.Text = "□华商报无忧招聘 □中华英才网 √智联招聘 □其他( )";
}
if (dat.Rows[]["xinxilaiyuan"].ToString().Equals("其他"))
{
newTable.Cell(, ).Range.Text = "□华商报无忧招聘 □中华英才网 □智联招聘 √其他( )";
}
newTable.Cell(, ).Merge(newTable.Cell(, )); //填充表格内容 第九行
newTable.Cell(, ).Range.Text = "工作意向";
newTable.Cell(, ).Merge(newTable.Cell(, )); newTable.Cell(, ).Merge(newTable.Cell(, )); newTable.Cell(, ).Range.Text = "期望的工作岗位及职务:" + dat.Rows[]["qiwangwork"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
//填充表格内容 第十行 newTable.Cell(, ).Range.Text = "期望薪资待遇:" + dat.Rows[]["qiwangmoney"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f; //填充表格内容 第十一行 newTable.Cell(, ).Range.Text = "教育背景";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "说 明";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "学习方式:1.统招;2.电大、夜大、函授、自考、成人(教育部承认学历);3.党校; 请在“学习方式”一栏中填入。如无此教育背景则对应行信息不填。";
newTable.Cell(, ).Merge(newTable.Cell(, )); //填充表格内容 第十二行 newTable.Cell(, ).Range.Text = "学 历";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "学 位";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "学习方式";
newTable.Cell(, ).Range.Text = "毕业院校";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "专 业";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "就读起止时间";
newTable.Cell(, ).Merge(newTable.Cell(, )); //填充表格内容 第13行
newTable.Cell(, ).Range.Text = "高 中/中 专";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (getXueLi("高中").Rows.Count > )
{
System.Data.DataRow dr = getXueLi("高中").Rows[];
newTable.Cell(, ).Range.Text = dr["xuewei"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["xuexifs"].ToString();
newTable.Cell(, ).Range.Text = dr["xuexiaoname"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["zhuanye"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = DateTime.Parse(dr["t1"].ToString()).ToString("yyyy.MM")
+ " 至 " + DateTime.Parse(dr["t2"].ToString()).ToString("yyyy.MM"); newTable.Cell(, ).Merge(newTable.Cell(, ));
}
else
{
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
} //填充表格内容 第14行 newTable.Cell(, ).Range.Text = "大 专";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
if (getXueLi("大专").Rows.Count > )
{
System.Data.DataRow dr = getXueLi("大专").Rows[];
newTable.Cell(, ).Range.Text = dr["xuewei"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["xuexifs"].ToString();
newTable.Cell(, ).Range.Text = dr["xuexiaoname"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["zhuanye"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = DateTime.Parse(dr["t1"].ToString()).ToString("yyyy.MM")
+ " 至 " + DateTime.Parse(dr["t2"].ToString()).ToString("yyyy.MM");
newTable.Cell(, ).Merge(newTable.Cell(, ));
}
else
{
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
} //填充表格内容 第15行 newTable.Cell(, ).Range.Text = "本 科";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
if (getXueLi("本科").Rows.Count > )
{
System.Data.DataRow dr = getXueLi("本科").Rows[];
newTable.Cell(, ).Range.Text = dr["xuewei"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["xuexifs"].ToString();
newTable.Cell(, ).Range.Text = dr["xuexiaoname"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["zhuanye"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = DateTime.Parse(dr["t1"].ToString()).ToString("yyyy.MM") +
" 至 " + DateTime.Parse(dr["t2"].ToString()).ToString("yyyy.MM");
newTable.Cell(, ).Merge(newTable.Cell(, ));
}
else
{
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
} //填充表格内容 第16行 newTable.Cell(, ).Range.Text = "第二学士 学位班";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (getXueLi("第二学士学位班").Rows.Count > )
{
System.Data.DataRow dr = getXueLi("第二学士学位班").Rows[];
newTable.Cell(, ).Range.Text = dr["xuewei"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["xuexifs"].ToString();
newTable.Cell(, ).Range.Text = dr["xuexiaoname"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["zhuanye"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = DateTime.Parse(dr["t1"].ToString()).ToString("yyyy.MM") +
" 至 " + DateTime.Parse(dr["t2"].ToString()).ToString("yyyy.MM");
newTable.Cell(, ).Merge(newTable.Cell(, )); }
else
{
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
} //填充表格内容 第17行 newTable.Cell(, ).Range.Text = "硕 士 研究生";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (getXueLi("硕士研究生").Rows.Count > )
{
System.Data.DataRow dr = getXueLi("硕士研究生").Rows[];
newTable.Cell(, ).Range.Text = dr["xuewei"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["xuexifs"].ToString();
newTable.Cell(, ).Range.Text = dr["xuexiaoname"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["zhuanye"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = DateTime.Parse(dr["t1"].ToString()).ToString("yyyy.MM") +
" 至 " + DateTime.Parse(dr["t2"].ToString()).ToString("yyyy.MM");
newTable.Cell(, ).Merge(newTable.Cell(, )); }
else
{
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
} // 填充表格内容 第18行 newTable.Cell(, ).Range.Text = "博 士 研究生";
newTable.Cell(, ).Merge(newTable.Cell(, ));
if (getXueLi("博士研究生").Rows.Count > )
{
System.Data.DataRow dr = getXueLi("博士研究生").Rows[];
newTable.Cell(, ).Range.Text = dr["xuewei"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["xuexifs"].ToString();
newTable.Cell(, ).Range.Text = dr["xuexiaoname"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dr["zhuanye"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = DateTime.Parse(dr["t1"].ToString()).ToString("yyyy.MM") +
" 至 " + DateTime.Parse(dr["t2"].ToString()).ToString("yyyy.MM");
newTable.Cell(, ).Merge(newTable.Cell(, )); }
else
{
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "";
newTable.Cell(, ).Merge(newTable.Cell(, ));
} // 填充表格内容 第19行 newTable.Cell(, ).Range.Text = "技术职称";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["zhicheng"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "职业资格";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["renzhizige"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = "其他资质及培训证书等";
newTable.Cell(, ).Merge(newTable.Cell(, ));
newTable.Cell(, ).Range.Text = dat.Rows[]["zhengshu"].ToString();
newTable.Cell(, ).Merge(newTable.Cell(, )); WordApp.ActiveDocument.PageSetup.SectionStart = Microsoft.Office.Interop.Word.WdSectionStart.wdSectionNewPage;//节的起始位置:新建页 newTable.Cell(, ).Select();//选中一行
// WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
object missing1 = System.Reflection.Missing.Value;
object count1 = ;
object WdLine1 = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
WordApp.Selection.MoveDown(ref WdLine1, ref count1, ref missing1);//光标向下移1行
WordApp.Selection.MoveDown(ref WdLine1, ref count1, ref missing1);//光标向下移1行
WordApp.Selection.TypeParagraph();//在表格外回车 //文档中创建表格
Microsoft.Office.Interop.Word.Table newTable1 = WordDoc.Tables.Add(WordApp.Selection.Range, , , ref Nothing, ref Nothing);
//设置表格框
newTable1.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable1.Columns[].Width = 25f;
newTable1.Columns[].Width = 40f;
newTable1.Columns[].Width = 40f;
newTable1.Columns[].Width = 30f;
newTable1.Columns[].Width = 50f;
newTable1.Columns[].Width = 70f;
newTable1.Columns[].Width = 70f;
newTable1.Columns[].Width = 130f;
newTable1.Columns[].Width = 90f; newTable1.Cell(, ).Range.Bold = ;//设置单元格中字体为粗体 WordApp.Selection.ParagraphFormat.LineSpacing = 10.5f; WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
//填充表格内容
for (int i = ; i < ; i++)
{
newTable1.Rows.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; }
for (int i = ; i < ; i++)
{
newTable1.Columns.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; } WordApp.Selection.MoveEnd(ref unit, ref countjz);
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 //填充表格内容 第1行
newTable1.Cell(, ).Range.Text = "工作经历";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Range.Text = "说 明";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Range.Text = "1、请按工作时间由近至远(倒序)填写; 2、如在同一家公司因岗位、级别、地点发生变化请予以证明,详细填写。";
newTable1.Cell(, ).Merge(newTable1.Cell(, )); //填充表格内容 第2行
newTable1.Cell(, ).Range.Text = "起止时间 (精确到月)";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Range.Text = "单位名称";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Range.Text = "岗 位/职 务";
newTable1.Cell(, ).Range.Text = "主要职责(20字以内)";
newTable1.Cell(, ).Range.Text = "证明人/联系方式"; string sqlgz = "select * from hr_quanzhigongzuo where id in ( select top 6 " +
" id from hr_quanzhigongzuo where uid='" + uid + "' order by id desc ) order by id asc ";
DataTable datgz = ZWL.DBUtility.MyDBHelp.GetDataTable(sqlgz);
if (datgz.Rows.Count > )
{ for (int i = ; i < datgz.Rows.Count; i++)
{
newTable1.Cell(i + , ).Range.Text = DateTime.Parse(datgz.Rows["t1"].ToString()).ToString("yyyy.MM") + " 至 " +
DateTime.Parse(datgz.Rows["t2"].ToString()).ToString("yyyy.MM");
newTable1.Cell(i + , ).Merge(newTable1.Cell(i + , ));
newTable1.Cell(i + , ).Range.Text = datgz.Rows["gongzuodanwei"].ToString();
newTable1.Cell(i + , ).Merge(newTable1.Cell(i + , ));
newTable1.Cell(i + , ).Range.Text = datgz.Rows["zhiwu"].ToString();
newTable1.Cell(i + , ).Range.Text = datgz.Rows["zhuyao"].ToString();
newTable1.Cell(i + , ).Range.Text = datgz.Rows["zhengming"].ToString();
} for (int i = datgz.Rows.Count + ; i < ; i++)
{
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
newTable1.Cell(i, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
} }
else
{
for (int i = ; i < ; i++)
{
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
newTable1.Cell(i, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
} } //填充表格内容 第9行
newTable1.Cell(, ).Range.Text = "家庭成员";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Range.Text = "关 系";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
//WordApp.Selection.MoveDown(ref WdLine1, ref count1, ref missing1);//光标向下移1行 newTable1.Cell(, ).Range.Text = "姓 名";
newTable1.Cell(, ).Range.Text = "出生日期";
newTable1.Cell(, ).Range.Text = "政治面貌";
newTable1.Cell(, ).Range.Text = "工作单位及职务(如退休填写退休前单位)";
newTable1.Cell(, ).Merge(newTable1.Cell(, )); string sqljt = " select top 3 * from hr_jiating where uid='" + uid + "' ";
DataTable datjt = ZWL.DBUtility.MyDBHelp.GetDataTable(sqljt); if (datjt.Rows.Count > )
{ for (int i = ; i < datjt.Rows.Count; i++)
{
newTable1.Cell(i + , ).Range.Text = datjt.Rows["guanli"].ToString();
newTable1.Cell(i + , ).Merge(newTable1.Cell(i + , ));
newTable1.Cell(i + , ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
newTable1.Cell(i + , ).Range.Text = datjt.Rows["name"].ToString();
newTable1.Cell(i + , ).Range.Text = DateTime.Parse(datjt.Rows["createdate"].ToString()).ToString("yyyy.MM");
newTable1.Cell(i + , ).Range.Text = datjt.Rows["zhengzhi"].ToString();
newTable1.Cell(i + , ).Range.Text = datjt.Rows["gongzuodanwei"].ToString();
newTable1.Cell(i + , ).Merge(newTable1.Cell(i + , ));
} for (int i = datjt.Rows.Count + ; i < ; i++)
{
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
newTable1.Cell(i, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
} }
else
{
for (int i = ; i < ; i++)
{
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
newTable1.Cell(i, ).Select();//选中一行
WordApp.Selection.ParagraphFormat.LineSpacing = 25f;
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Range.Text = "";
newTable1.Cell(i, ).Merge(newTable1.Cell(i, ));
} } //填充表格内容 第13行
newTable1.Cell(, ).Range.Text = "自我鉴定";
newTable1.Cell(, ).Range.Text = dat.Rows[]["jianding"].ToString();
newTable1.Cell(, ).Merge(newTable1.Cell(, )); //填充表格内容 第14行
newTable1.Cell(, ).Range.Text = "劳动合同签订情况";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
if (dat.Rows[]["iflizhi"].ToString().Equals(""))
{
newTable1.Cell(, ).Range.Text = "√目前与其他单位不存在劳动关系; " +
"□目前与原单位尚未解除劳动合同,但承诺最迟在到岗时可以提供解除劳动合同证明;" +
"□其他(请说明)";
}
else if (dat.Rows[]["iflizhi"].ToString().Equals(""))
{
newTable1.Cell(, ).Range.Text = "□目前与其他单位不存在劳动关系; " +
"√目前与原单位尚未解除劳动合同,但承诺最迟在到岗时可以提供解除劳动合同证明;" +
"□其他(请说明)";
}
else
{
newTable1.Cell(, ).Range.Text = "□目前与其他单位不存在劳动关系; " +
"□目前与原单位尚未解除劳动合同,但承诺最迟在到岗时可以提供解除劳动合同证明;" +
"√其他(" + dat.Rows[]["iflizhi"].ToString() + ")";
}
newTable1.Cell(, ).Merge(newTable1.Cell(, )); string sqlyp = " select date from hr_yingpin where uid = '" + uid + "'";
string nian = "";
DataTable datyp = ZWL.DBUtility.MyDBHelp.GetDataTable(sqlyp);
if (datyp.Rows.Count > )
{
nian = DateTime.Parse(datyp.Rows[]["date"].ToString()).ToString("yyyy年MM月dd日");
}
//填充表格内容 第15行
newTable1.Cell(, ).Range.Text = "个人声明";
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Merge(newTable1.Cell(, ));
newTable1.Cell(, ).Range.Text = " 本人郑重声明,我在上述表中提的有关信息真实准确,并愿意承担因任何虚假与不实造成的一切后果;本人同意可在必要的情况下对有关信息进行核实。";
newTable1.Cell(, ).Merge(newTable1.Cell(, )); //填充表格内容 第16行 newTable1.Cell(, ).Range.Text = " 此外,我清楚以下事实:如果在30天内没有接到面试通知,本次申请可能失效,个人简历将进入公司人才库,不予退回。";
newTable1.Cell(, ).Merge(newTable1.Cell(, )); //填充表格内容 第17行 newTable1.Cell(, ).Range.Text = " 应聘者签名:" + dat.Rows[]["name"].ToString() + " " + nian;
newTable1.Cell(, ).Merge(newTable1.Cell(, )); object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Application.Quit(ref saveOption, ref Nothing, ref Nothing);
WordDoc = null;
WordApp = null;
killAllProcess();
message = name + "文档生成成功,";
//}
//catch
//{
// message = "文件导出异常!";
//}
return message;
}
public DataTable getXueLi(string xueli)
{
string sql = "select * from hr_jiaoyu where uid='" + dat.Rows[]["id"].ToString() + "' and xueli ='" + xueli + "' ";
DataTable datxue = ZWL.DBUtility.MyDBHelp.GetDataTable(sql);
return datxue;
}
protected void killAllProcess() // 杀掉所有winword.exe进程
{
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in myPs)
{
if (p.Id != )
{
string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
try
{
if (p.Modules != null)
if (p.Modules.Count > )
{
System.Diagnostics.ProcessModule pm = p.Modules[];
myS += "\n Modules[0].FileName:" + pm.FileName;
myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
if (pm.ModuleName.ToLower() == "winword.exe")
p.Kill();
}
}
catch
{ }
finally
{
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
} }

调用方法:

 private void Export(string id)
{
string wjname =DateTime.Now.ToString("yyyyMMddhhmmssfff");
string filepath = Server.MapPath("./ReportFile/") + wjname;
Directory.CreateDirectory(filepath); //创建文件所在目录
ExportWord dao = new ExportWord();
if (id.IndexOf(',') > -)
{ string[] strs = id.Split(',');
for (int i = ; i < strs.Length; i++)
{
string sql = " select name from hr_person where id =" + strs;
DataTable dat = ZWL.DBUtility.MyDBHelp.GetDataTable(sql);
dao.ProcessRequest(Context, strs, filepath + "/" + dat.Rows[]["name"].ToString() +i+ ".doc");
}
//多个ID,所以这边调用压缩方法
DirectoryToZip(filepath, Server.MapPath("./ReportFile/" + wjname + ".zip"));
Response.Write("<script>window.location='ReportFile/" + wjname + ".zip'</script>"); }
else
{
string sql = " select name from hr_person where id =" + id;
DataTable dat = ZWL.DBUtility.MyDBHelp.GetDataTable(sql);
dao.ProcessRequest(Context, id, filepath + "/" +dat.Rows[]["name"].ToString() + ".doc");
Response.Write("<script>window.location='ReportFile/" + wjname+"/" + dat.Rows[]["name"].ToString() + ".doc'</script>"); } }
//【filepath想要压缩文件的地址】
//【zippath输出压缩文件的地址】
private void DirectoryToZip(string path, string address)
{
//获取当前文件夹中所有的文件
string[] filenames = Directory.GetFiles(path);
Crc32 crc = new Crc32();
//创建输出文件(ZIP格式的文件)
ZipOutputStream zos = new ZipOutputStream(File.Create(address));
zos.SetLevel();
//遍历所有的文件
foreach (string name in filenames)
{
FileStream fs = File.OpenRead(name);
byte[] buffer = new byte[fs.Length];
//读取文件
fs.Read(buffer, , Convert.ToInt32(fs.Length));
//获取文件的文件名称和后缀名
string file = Path.GetFileName(name);
//输出文件的名称
ZipEntry entry = new ZipEntry(file);
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zos.PutNextEntry(entry);
zos.Write(buffer, , Convert.ToInt32(fs.Length));
fs.Close();
}
zos.Finish();
zos.Close();
}

有什么问大家可以讨论,讨论,我也只是研究了一周,有许多都是 ,网上找其他人的,代码是长了点,但是只要把前面的看懂了就行,后面的只是重复前面的代码,效果如图片

c#(.net)   导出   word表格

c#(.net)   导出   word表格

c#(.net)   导出   word表格

c#(.net)   导出   word表格

c#(.net)   导出   word表格