private static void word_init(XWPFDocument m_Docx, String prjName) { //设置页面 将页面设置为A4 纵向 //参考网站 http://www.aiuxian.com/article/p-1970779.html CT_SectPr m_SectPr = new CT_SectPr(); m_SectPr.pgSz.w = (ulong)11906; m_SectPr.pgSz.h = (ulong)16838; m_Docx.Document.body.sectPr = m_SectPr; //第一页 封面 word_insert_space(5, m_Docx); word_insert_text(m_Docx, "宋体", 22, prjName); word_insert_text(m_Docx, "宋体", 22, "GSM-R 通信系统"); word_insert_text(m_Docx, "宋体", 22, "现场勘查报告"); word_insert_space(7, m_Docx); word_insert_text(m_Docx, "宋体", 22, DateTime.Now.ToString("yyyy年MM月dd日")); word_insert_space(7, m_Docx); //创建表并获取该表 XWPFTable table = m_Docx.CreateTable(4, 2); CT_Tbl ctbl = m_Docx.Document.body.GetTblArray()[0]; //表居中 ctbl.AddNewTblPr().jc = new CT_Jc(); ctbl.AddNewTblPr().jc.val = ST_Jc.center; //表宽度为8000 ctbl.AddNewTblPr().AddNewTblW().w = "8000"; ctbl.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa; //列宽设置 CT_TcPr m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); m_Pr.tcW = new CT_TblWidth(); m_Pr.tcW.w = "3000"; m_Pr.tcW.type = ST_TblWidth.dxa; //行高设置 //设置表中文本 table.GetRow(0).GetCell(0).SetText("项目"); table.GetRow(1).GetCell(0).SetText("勘察日期"); table.GetRow(2).GetCell(0).SetText("现场勘查人员"); table.GetRow(3).GetCell(0).SetText("报告修正人员"); word_insert_space(4, m_Docx); }
private void CreateEmptyTable(CT_Tbl table) { // MINIMUM ELEMENTS FOR A TABLE table.AddNewTr().AddNewTc().AddNewP(); CT_TblPr tblpro = table.AddNewTblPr(); if (!tblpro.IsSetTblW()) { tblpro.AddNewTblW().w = "0"; } tblpro.tblW.type = (ST_TblWidth.auto); // layout tblpro.AddNewTblLayout().type = ST_TblLayoutType.autofit; // borders CT_TblBorders borders = tblpro.AddNewTblBorders(); borders.AddNewBottom().val = ST_Border.single; borders.AddNewInsideH().val = ST_Border.single; borders.AddNewInsideV().val = ST_Border.single; borders.AddNewLeft().val = ST_Border.single; borders.AddNewRight().val = ST_Border.single; borders.AddNewTop().val = ST_Border.single; CT_TblGrid tblgrid = table.AddNewTblGrid(); tblgrid.AddNewGridCol().w = (ulong)2000; }
private void CreateEmptyTable(CT_Tbl table) { // MINIMUM ELEMENTS FOR A TABLE table.AddNewTr().AddNewTc().AddNewP(); CT_TblPr tblpro = table.AddNewTblPr(); if (!tblpro.IsSetTblW()) { tblpro.AddNewTblW().w = "0"; } tblpro.tblW.type = (ST_TblWidth.auto); // layout // tblpro.AddNewTblLayout().Type=(STTblLayoutType.AUTOFIT); // borders CT_TblBorders borders = tblpro.AddNewTblBorders(); borders.AddNewBottom().val = (ST_Border.single); borders.AddNewInsideH().val = (ST_Border.single); borders.AddNewInsideV().val = (ST_Border.single); borders.AddNewLeft().val = (ST_Border.single); borders.AddNewRight().val = (ST_Border.single); borders.AddNewTop().val = (ST_Border.single); /* * CTTblGrid tblgrid=table.AddNewTblGrid(); * tblgrid.AddNewGridCol().W=(new Bigint("2000")); */ GetRows(); }
/// <summary> /// word 插入表格功能(13行2列) /// </summary> /// <param name="m_Docx">根文档</param> /// <param name="device_type">设备类型</param> /// <param name="kilometer_mark">公里标</param> /// <param name="side_direction">下行侧向</param> /// <param name="longitude">经度</param> /// <param name="latitude">纬度</param> private static void word_inster_table(XWPFDocument m_Docx, DbBean bean, int i = 1) { XWPFTable table = m_Docx.CreateTable(12, 2); CT_Tbl ctbl = m_Docx.Document.body.GetTblArray()[i]; CT_TblPr ctblpr = ctbl.AddNewTblPr(); ctblpr.jc = new CT_Jc(); ctblpr.jc.val = ST_Jc.center; table.Width = 3500; table.GetRow(0).GetCell(0).SetText("设备类型"); table.GetRow(0).GetCell(1).SetText(bean.DeviceType); table.GetRow(1).GetCell(0).SetText("公里标"); table.GetRow(1).GetCell(1).SetText(bean.KilometerMark); table.GetRow(2).GetCell(0).SetText("下行侧向"); table.GetRow(2).GetCell(1).SetText(bean.SideDirection); table.GetRow(3).GetCell(0).SetText("距线路中心距离(m)"); table.GetRow(4).GetCell(0).SetText("经度"); table.GetRow(4).GetCell(1).SetText(bean.Longitude); table.GetRow(5).GetCell(0).SetText("纬度"); table.GetRow(5).GetCell(1).SetText(bean.Latitude); table.GetRow(6).GetCell(0).SetText("杆塔类型"); table.GetRow(6).GetCell(1).SetText(bean.TowerType); table.GetRow(7).GetCell(0).SetText("杆塔高度"); table.GetRow(7).GetCell(1).SetText(bean.TowerHeight); table.GetRow(8).GetCell(0).SetText("天线1方向角"); table.GetRow(8).GetCell(1).SetText(bean.AntennaDirection1); table.GetRow(9).GetCell(0).SetText("天线2方向角"); table.GetRow(9).GetCell(1).SetText(bean.AntennaDirection2); table.GetRow(10).GetCell(0).SetText("天线3方向角"); table.GetRow(10).GetCell(1).SetText(bean.AntennaDirection3); table.GetRow(11).GetCell(0).SetText("天线4方向角"); table.GetRow(11).GetCell(1).SetText(bean.AntennaDirection4); CT_TcPr m_Pr = table.GetRow(2).GetCell(1).GetCTTc().AddNewTcPr(); m_Pr.tcW = new CT_TblWidth(); m_Pr.tcW.w = "3500"; m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度 XWPFTableRow m_Row = table.InsertNewTableRow(0); XWPFTableCell cell = m_Row.CreateCell(); CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctPr = cttc.AddNewTcPr(); ctPr.gridSpan = new CT_DecimalNumber(); ctPr.gridSpan.val = "2"; cttc.GetPList()[0].AddNewR().AddNewT().Value = "SITE 【序号】"; word_insert_space(1, m_Docx, 100); word_insert_text(m_Docx, "宋体", 11, "SITE 【序号】勘站照片"); word_insert_text(m_Docx, "宋体", 11, "(3-10张照片)"); word_insert_space(1, m_Docx, 100); }
public void TestSetGetWidth() { XWPFDocument doc = new XWPFDocument(); CT_Tbl table = new CT_Tbl(); table.AddNewTblPr().AddNewTblW().w = "1000"; XWPFTable xtab = new XWPFTable(table, doc); Assert.AreEqual(1000, xtab.GetWidth()); xtab.SetWidth(100); Assert.AreEqual(100, int.Parse(table.tblPr.tblW.w)); }
/// <summary> /// 生成模板文件 /// </summary> /// <param name="m_Docx">根文档</param> private static void word_init(XWPFDocument m_Docx) { //1‘=1440twip=25.4mm=72pt(磅point)=96px(像素pixel) //1px(像素pixel)=0.75pt(磅point) // A4:W=11906 twip=8.269''=210mm,h=16838twip=11.693''=297mm //A5:W=8390 twip=5.827''=148mm,h=11906 twip=8.269''=210mm //A6:W=5953 twip=4.134''=105mm,h=8390twip=5.827''=1148mm //16k195mmX270mm: //16k184mmX260mm: //16k197mmX273mm: CT_SectPr m_SectPr = new CT_SectPr(); //页面设置A4纵向 m_SectPr.pgSz.w = (ulong)11906; m_SectPr.pgSz.h = (ulong)16838; m_Docx.Document.body.sectPr = m_SectPr; //第一页 word_insert_space(4, m_Docx); word_insert_text(m_Docx, "宋体", 22, "【项目名称】"); word_insert_text(m_Docx, "宋体", 22, "GSM-R 通信系统"); word_insert_text(m_Docx, "宋体", 22, "现场勘查报告"); word_insert_space(8, m_Docx); word_insert_text(m_Docx, "宋体", 22, "【日期】"); word_insert_space(7, m_Docx); //第二页 //表1 XWPFTable table = m_Docx.CreateTable(4, 2); CT_Tbl ctbl = m_Docx.Document.body.GetTblArray()[0]; CT_TblPr ctblpr = ctbl.AddNewTblPr(); ctblpr.jc = new CT_Jc(); ctblpr.jc.val = ST_Jc.center; table.Width = 4000; table.GetRow(0).GetCell(0).SetText("项目"); table.GetRow(1).GetCell(0).SetText("日期"); table.GetRow(2).GetCell(0).SetText("现场勘查人员"); table.GetRow(3).GetCell(0).SetText("报告编制人员"); CT_TcPr m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); m_Pr.tcW = new CT_TblWidth(); m_Pr.tcW.w = "4000"; m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度 word_insert_space(2, m_Docx); word_insert_text(m_Docx, "宋体", 12, "基站勘察表"); }
private void CreateEmptyTable(CT_Tbl table) { table.AddNewTr().AddNewTc().AddNewP(); CT_TblPr ctTblPr = table.AddNewTblPr(); if (!ctTblPr.IsSetTblW()) { ctTblPr.AddNewTblW().w = "0"; } ctTblPr.tblW.type = ST_TblWidth.auto; CT_TblBorders ctTblBorders = ctTblPr.AddNewTblBorders(); ctTblBorders.AddNewBottom().val = ST_Border.single; ctTblBorders.AddNewInsideH().val = ST_Border.single; ctTblBorders.AddNewInsideV().val = ST_Border.single; ctTblBorders.AddNewLeft().val = ST_Border.single; ctTblBorders.AddNewRight().val = ST_Border.single; ctTblBorders.AddNewTop().val = ST_Border.single; this.GetRows(); }
private void CreateEmptyTable(CT_Tbl table) { // MINIMUM ELEMENTS FOR A TABLE table.AddNewTr().AddNewTc().AddNewP(); CT_TblPr tblpro = table.AddNewTblPr(); if (!tblpro.IsSetTblW()) tblpro.AddNewTblW().w = "0"; tblpro.tblW.type=(ST_TblWidth.auto); // layout // tblpro.AddNewTblLayout().Type=(STTblLayoutType.AUTOFIT); // borders CT_TblBorders borders = tblpro.AddNewTblBorders(); borders.AddNewBottom().val=(ST_Border.single); borders.AddNewInsideH().val = (ST_Border.single); borders.AddNewInsideV().val = (ST_Border.single); borders.AddNewLeft().val = (ST_Border.single); borders.AddNewRight().val = (ST_Border.single); borders.AddNewTop().val = (ST_Border.single); /* * CTTblGrid tblgrid=table.AddNewTblGrid(); * tblgrid.AddNewGridCol().W=(new Bigint("2000")); */ GetRows(); }
private CT_TblPr GetTrPr() { return((ctTbl.tblPr != null) ? ctTbl.tblPr : ctTbl .AddNewTblPr()); }
public void TestSetGetWidth() { XWPFDocument doc = new XWPFDocument(); CT_Tbl table = new CT_Tbl(); table.AddNewTblPr().AddNewTblW().w = "1000"; XWPFTable xtab = new XWPFTable(table, doc); Assert.AreEqual(1000, xtab.Width); xtab.Width = 100; Assert.AreEqual(100, int.Parse(table.tblPr.tblW.w)); }
private static void word_inster_table(XWPFDocument m_Docx, OutputData bean, int i, String photoPathName) { XWPFTable table = m_Docx.CreateTable(12, 2); CT_Tbl ctbl = m_Docx.Document.body.GetTblArray()[i]; CT_TblPr ctblpr = ctbl.AddNewTblPr(); ctblpr.jc = new CT_Jc(); ctblpr.jc.val = ST_Jc.center; table.Width = 3500; table.GetRow(0).GetCell(0).SetText("设备类型"); table.GetRow(0).GetCell(1).SetText(bean.DeviceType); table.GetRow(1).GetCell(0).SetText("公里标"); table.GetRow(1).GetCell(1).SetText(bean.KilometerMark); table.GetRow(2).GetCell(0).SetText("下行侧向"); table.GetRow(2).GetCell(1).SetText(bean.SideDirection); table.GetRow(3).GetCell(0).SetText("距线路中心距离(m)"); table.GetRow(4).GetCell(0).SetText("经度"); table.GetRow(4).GetCell(1).SetText(bean.Longitude); table.GetRow(5).GetCell(0).SetText("纬度"); table.GetRow(5).GetCell(1).SetText(bean.Latitude); table.GetRow(6).GetCell(0).SetText("杆塔类型"); table.GetRow(6).GetCell(1).SetText(bean.TowerType); table.GetRow(7).GetCell(0).SetText("杆塔高度"); table.GetRow(7).GetCell(1).SetText(bean.TowerHeight); table.GetRow(8).GetCell(0).SetText("天线1方向角"); table.GetRow(8).GetCell(1).SetText(bean.AntennaDirection1); table.GetRow(9).GetCell(0).SetText("天线2方向角"); table.GetRow(9).GetCell(1).SetText(bean.AntennaDirection2); table.GetRow(10).GetCell(0).SetText("天线3方向角"); table.GetRow(10).GetCell(1).SetText(bean.AntennaDirection3); table.GetRow(11).GetCell(0).SetText("天线4方向角"); table.GetRow(11).GetCell(1).SetText(bean.AntennaDirection4); CT_TcPr m_Pr = table.GetRow(2).GetCell(1).GetCTTc().AddNewTcPr(); m_Pr.tcW = new CT_TblWidth(); m_Pr.tcW.w = "3500"; m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度 XWPFTableRow m_Row = table.InsertNewTableRow(0); XWPFTableCell cell = m_Row.CreateCell(); CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctPr = cttc.AddNewTcPr(); ctPr.gridSpan = new CT_DecimalNumber(); ctPr.gridSpan.val = "2"; cttc.GetPList()[0].AddNewR().AddNewT().Value = "SITE: " + bean.MarkerId; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(photoPathName); System.IO.FileInfo[] files = dir.GetFiles(); foreach (System.IO.FileInfo file in files) { FileStream gfs = new FileStream(photoPathName + "\\" + file.Name, FileMode.Open, FileAccess.Read); Image image = Image.FromFile(photoPathName + "\\" + file.Name); Double ratio = (Double)image.Width / (Double)image.Height; image.Dispose(); XWPFParagraph gp = m_Docx.CreateParagraph(); gp.SetAlignment(ParagraphAlignment.CENTER); XWPFRun gr = gp.CreateRun(); if (ratio > 1) { gr.AddPicture(gfs, (int)PictureType.JPEG, file.Name, 3555556, 2000000); } else { gr.AddPicture(gfs, (int)PictureType.JPEG, file.Name, 2000000, 3555556); } gfs.Close(); } word_insert_space(3, m_Docx, 100); }
private void CreateEmptyTable(CT_Tbl table) { // MINIMUM ELEMENTS FOR A TABLE table.AddNewTr().AddNewTc().AddNewP(); CT_TblPr tblpro = table.AddNewTblPr(); if (!tblpro.IsSetTblW()) tblpro.AddNewTblW().w = "0"; tblpro.tblW.type=(ST_TblWidth.auto); // layout tblpro.AddNewTblLayout().type = ST_TblLayoutType.autofit; // borders CT_TblBorders borders = tblpro.AddNewTblBorders(); borders.AddNewBottom().val=ST_Border.single; borders.AddNewInsideH().val = ST_Border.single; borders.AddNewInsideV().val = ST_Border.single; borders.AddNewLeft().val = ST_Border.single; borders.AddNewRight().val = ST_Border.single; borders.AddNewTop().val = ST_Border.single; CT_TblGrid tblgrid=table.AddNewTblGrid(); tblgrid.AddNewGridCol().w= (ulong)2000; }
private void FlightPlanDataExport(List <string> planlist) { AjaxResult result = new AjaxResult(); result.IsSuccess = true; XWPFDocument doc = new XWPFDocument(); CT_SectPr m_SectPr = new CT_SectPr(); //设置宽度(这里是一个ulong类型) m_SectPr.pgSz.w = 16838; //设置高度(这里是一个ulong类型) m_SectPr.pgSz.h = 11906; m_SectPr.pgMar.left = (ulong)300; //左边距 m_SectPr.pgMar.right = (ulong)300; //右边距 m_SectPr.pgMar.top = "300"; //上边距 m_SectPr.pgMar.bottom = "300"; //下边距 //设置页面的尺寸 doc.Document.body.sectPr = m_SectPr; XWPFParagraph p0 = doc.CreateParagraph(); //创建段落 p0.Alignment = ParagraphAlignment.CENTER; //居中显示 XWPFRun r0 = p0.CreateRun(); //设置字体 r0.FontFamily = "宋体"; //设置字体大小 r0.FontSize = 16; //字体是否加粗,这里加粗了 r0.IsBold = true; r0.SetText("武汉管制分区(河南地区)通用航空计划表"); //写入文本 XWPFParagraph p1 = doc.CreateParagraph(); //创建段落 p1.Alignment = ParagraphAlignment.CENTER; //居中显示 XWPFRun r1 = p1.CreateRun(); //设置字体 r1.FontFamily = "宋体"; //设置字体大小 r1.FontSize = 16; //字体是否加粗,这里加粗了 r1.IsBold = true; r1.SetText("武基范围内");//写入文本 var wordlist = new ExportDataBLL().FlightPlanDataExport(planlist); XWPFTable table = doc.CreateTable(2 + wordlist.Count, 13); table.SetColumnWidth(0, 4 * 256);//设置列的宽度 table.SetColumnWidth(1, 4 * 256); table.SetColumnWidth(2, 4 * 256); table.SetColumnWidth(3, 4 * 256); table.SetColumnWidth(4, 4 * 256); table.SetColumnWidth(5, 20 * 256); table.SetColumnWidth(6, 4 * 256); table.SetColumnWidth(7, 4 * 256); table.SetColumnWidth(8, 3 * 256); table.SetColumnWidth(9, 3 * 256); table.SetColumnWidth(10, 3 * 256); table.SetColumnWidth(11, 3 * 256); table.SetColumnWidth(12, 2 * 256); CT_Tbl m_CTTbl = doc.Document.body.GetTblArray()[0];//获得文档第一张表 m_CTTbl.AddNewTblPr().jc = new CT_Jc(); m_CTTbl.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 table.GetRow(0).Height = 1200; setCell(table.GetRow(0).GetCell(0), "序号"); setCell(table.GetRow(0).GetCell(1), "起降场"); setCell(table.GetRow(0).GetCell(2), "单位"); setCell(table.GetRow(0).GetCell(3), "机型/架数"); setCell(table.GetRow(0).GetCell(4), "机号"); setCell(table.GetRow(0).GetCell(5), "空域/航线"); setCell(table.GetRow(0).GetCell(6), "高度"); setCell(table.GetRow(0).GetCell(7), "任务"); setCell(table.GetRow(0).GetCell(8), "开飞时间"); setCell(table.GetRow(0).GetCell(10), "结束时间"); setCell(table.GetRow(0).GetCell(12), "备注"); table.GetRow(1).Height = 1200; setCell(table.GetRow(1).GetCell(8), "计划"); setCell(table.GetRow(1).GetCell(9), "实施"); setCell(table.GetRow(1).GetCell(10), "计划"); setCell(table.GetRow(1).GetCell(11), "实施"); mergeCellsVertically(table, 0, 0, 1); table.GetRow(0).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 1, 0, 1); table.GetRow(0).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 2, 0, 1); table.GetRow(0).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 3, 0, 1); table.GetRow(0).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 4, 0, 1); table.GetRow(0).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 5, 0, 1); table.GetRow(0).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 6, 0, 1); table.GetRow(0).GetCell(6).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 7, 0, 1); table.GetRow(0).GetCell(7).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsHorizontal(table, 0, 8, 9); table.GetRow(0).GetCell(8).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsHorizontal(table, 0, 10, 11); table.GetRow(0).GetCell(10).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); mergeCellsVertically(table, 12, 0, 1); table.GetRow(0).GetCell(12).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); table.GetRow(1).GetCell(8).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); table.GetRow(1).GetCell(9).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); table.GetRow(1).GetCell(10).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); table.GetRow(1).GetCell(11).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); for (int i = 0; i < wordlist.Count; i++) { setCell(table.GetRow(2 + i).GetCell(0), (i + 1).ToString()); setCell(table.GetRow(2 + i).GetCell(1), wordlist[i].airport); setCell(table.GetRow(2 + i).GetCell(2), wordlist[i].company); setCell(table.GetRow(2 + i).GetCell(3), wordlist[i].airtype); setCell(table.GetRow(2 + i).GetCell(4), ""); setCell(table.GetRow(2 + i).GetCell(5), wordlist[i].airline); setCell(table.GetRow(2 + i).GetCell(6), wordlist[i].high); setCell(table.GetRow(2 + i).GetCell(7), wordlist[i].messiontype); setCell(table.GetRow(2 + i).GetCell(8), wordlist[i].planbegin); setCell(table.GetRow(2 + i).GetCell(9), ""); setCell(table.GetRow(2 + i).GetCell(10), wordlist[i].planend); setCell(table.GetRow(2 + i).GetCell(11), ""); setCell(table.GetRow(2 + i).GetCell(12), wordlist[i].remark); } //var book = new HSSFWorkbook(); ////设置默认工作表的默认列宽和行高 //var sheet = book.CreateSheet("通航计划表-范围内"); //sheet.DefaultRowHeight = 20 * 20; //sheet.DefaultColumnWidth = 12; //sheet.SetColumnWidth(5, 60 * 256); ////设置标题样式 //var titleStyle = book.CreateCellStyle(); //titleStyle.Alignment = HorizontalAlignment.Center; //titleStyle.VerticalAlignment = VerticalAlignment.Center; //var titleFont = book.CreateFont(); //titleFont.FontHeightInPoints = 20; //titleFont.IsBold = true; //titleStyle.SetFont(titleFont); ////设置表头样式 //var headStyle = book.CreateCellStyle(); //headStyle.Alignment = HorizontalAlignment.Center; //headStyle.VerticalAlignment = VerticalAlignment.Center; //var headFont = book.CreateFont(); //headFont.FontHeightInPoints = 14; //headFont.IsBold = true; //headStyle.SetFont(headFont); ////设置标题样式 //var listStyle = book.CreateCellStyle(); //listStyle.Alignment = HorizontalAlignment.Center; //listStyle.VerticalAlignment = VerticalAlignment.Center; //var listFont = book.CreateFont(); //listFont.FontHeightInPoints = 14; //listStyle.SetFont(listFont); ////填入标题内容并合并 //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 12)); //var titleRow = sheet.CreateRow(0); //titleRow.CreateCell(0).SetCellValue("通航计划表-范围内"); //titleRow.Cells[0].CellStyle = titleStyle; //titleRow.Height = 50 * 20; ////填入表头内容并合并 //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 0, 0)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 1, 1)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 2, 2)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 3, 3)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 4, 4)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 5, 5)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 6, 6)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 7, 7)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 8, 9)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 10, 11)); //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 12, 12)); //var headRow1 = sheet.CreateRow(1); //headRow1.CreateCell(0).SetCellValue("序号"); //headRow1.CreateCell(1).SetCellValue("起降场"); //headRow1.CreateCell(2).SetCellValue("单位"); //headRow1.CreateCell(3).SetCellValue("机型/架次"); //headRow1.CreateCell(4).SetCellValue("机号"); //headRow1.CreateCell(5).SetCellValue("空域/航线"); //headRow1.CreateCell(6).SetCellValue("高度"); //headRow1.CreateCell(7).SetCellValue("任务"); //headRow1.CreateCell(8).SetCellValue("开飞时间"); //headRow1.CreateCell(10).SetCellValue("结束时间"); //headRow1.CreateCell(12).SetCellValue("备注"); //foreach (var cell in headRow1.Cells) //{ // cell.CellStyle = headStyle; //} //headRow1.Height = 25 * 20; //var headRow2 = sheet.CreateRow(2); //headRow2.CreateCell(8).SetCellValue("计划"); //headRow2.CreateCell(9).SetCellValue("实施"); //headRow2.CreateCell(10).SetCellValue("计划"); //headRow2.CreateCell(11).SetCellValue("实施"); //foreach (var cell in headRow2.Cells) //{ // cell.CellStyle = headStyle; //} //headRow2.Height = 25 * 20; //var exportlist = new ExportDataBLL().FlightPlanDataExport(planlist); //for (int i = 0; i < exportlist.Count; i++) //{ // var row = sheet.CreateRow(3 + i); // row.CreateCell(0).SetCellValue(i + 1); // row.CreateCell(1).SetCellValue(exportlist[i].airport); // row.CreateCell(2).SetCellValue(exportlist[i].company); // row.CreateCell(3).SetCellValue(exportlist[i].airtype); // row.CreateCell(4).SetCellValue(exportlist[i].aircraft); // row.CreateCell(5).SetCellValue(exportlist[i].airline); // row.CreateCell(6).SetCellValue(exportlist[i].high); // row.CreateCell(7).SetCellValue(exportlist[i].airtype); // row.CreateCell(8).SetCellValue(exportlist[i].planbegin); // row.CreateCell(10).SetCellValue(exportlist[i].planend); // row.CreateCell(12).SetCellValue(exportlist[i].remark); // foreach (var cell in row.Cells) // { // cell.CellStyle = listStyle; // } // //row.Height = 30 * 20; //} //生成文件 var file = new MemoryStream(); doc.Write(file); Response.ContentType = "application/msword"; Response.ContentEncoding = Encoding.UTF8; Response.Charset = ""; Response.Clear(); Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("通航计划表-范围内.doc", Encoding.UTF8)); file.WriteTo(Response.OutputStream); file.Close(); Response.End(); }
protected void Button1_Click(object sender, EventArgs e) { //图片位置 String m_PicPath = @"C:\ttt.jpeg"; FileStream gfs = null; MemoryStream ms = new MemoryStream(); XWPFDocument m_Docx = new XWPFDocument(); //页面设置 //A4:W=11906,h=16838 //CT_SectPr m_SectPr = m_Docx.Document.body.AddNewSectPr(); m_Docx.Document.body.sectPr = new CT_SectPr(); CT_SectPr m_SectPr = m_Docx.Document.body.sectPr; //页面设置A4纵向 m_SectPr.pgSz.h = (ulong)16838; m_SectPr.pgSz.w = (ulong)11906; XWPFParagraph gp = m_Docx.CreateParagraph(); // gp.GetCTPPr().AddNewJc().val = ST_Jc.center; //水平居中 XWPFRun gr = gp.CreateRun(); gr.GetCTR().AddNewRPr().AddNewRFonts().ascii = "黑体"; gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑体"; gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia; gr.GetCTR().AddNewRPr().AddNewSz().val = (ulong)44; //2号字体 gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)44; gr.GetCTR().AddNewRPr().AddNewB().val = true; //加粗 gr.GetCTR().AddNewRPr().AddNewColor().val = "red"; //字体颜色 gr.SetText("NPOI创建Word2007Docx"); gp = m_Docx.CreateParagraph(); //gp.GetCTPPr().AddNewJc().val = ST_Jc.both; // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符 gp.IndentationFirstLine = 15; gr = gp.CreateRun(); CT_RPr rpr = gr.GetCTR().AddNewRPr(); CT_Fonts rfonts = rpr.AddNewRFonts(); rfonts.ascii = "宋体"; rfonts.eastAsia = "宋体"; rpr.AddNewSz().val = (ulong)21;//5号字体 rpr.AddNewSzCs().val = (ulong)21; gr.SetText("NPOI,顾名思义,就是POI的.NET版本。那POI又是什么呢?POI是一套用Java写成的库,能够帮助开 发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等 。目前POI的稳定版本中支持Excel文件格式(xls和xlsx),其他的都属于不稳定版本(放在poi的scrachpad目录 中)。"); //创建表 XWPFTable table = m_Docx.CreateTable(1, 4); //创建一行4列表 CT_Tbl m_CTTbl = m_Docx.Document.body.GetTblArray()[0]; //获得文档第一张表 CT_TblPr m_CTTblPr = m_CTTbl.AddNewTblPr(); m_CTTblPr.AddNewTblW().w = "2000"; //表宽 m_CTTblPr.AddNewTblW().type = ST_TblWidth.dxa; m_CTTblPr.tblpPr = new CT_TblPPr(); //表定位 m_CTTblPr.tblpPr.tblpX = "4003"; //表左上角坐标 m_CTTblPr.tblpPr.tblpY = "365"; m_CTTblPr.tblpPr.tblpXSpec = ST_XAlign.center; //若不为“Null”,则优先tblpX,即表由tblpXSpec定位 m_CTTblPr.tblpPr.tblpYSpec = ST_YAlign.center; //若不为“Null”,则优先tblpY,即表由tblpYSpec定位 m_CTTblPr.tblpPr.leftFromText = (ulong)180; m_CTTblPr.tblpPr.rightFromText = (ulong)180; m_CTTblPr.tblpPr.vertAnchor = ST_VAnchor.text; m_CTTblPr.tblpPr.horzAnchor = ST_HAnchor.page; //表1行4列充值:a,b,c,d table.GetRow(0).GetCell(0).SetText("a"); table.GetRow(0).GetCell(1).SetText("b"); table.GetRow(0).GetCell(2).SetText("c"); table.GetRow(0).GetCell(3).SetText("d"); CT_Row m_NewRow = new CT_Row();//创建1行 XWPFTableRow m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); //必须要!!! XWPFTableCell cell = m_Row.CreateCell(); //创建单元格,也创建了一个CT_P CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctPr = cttc.AddNewTcPr(); //ctPr.gridSpan.val = "3";//合并3列 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cttc.GetPList()[0].AddNewR().AddNewT().Value = "666"; cell = m_Row.CreateCell();//创建单元格,也创建了一个CT_P cell.GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cell.GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "e"; //合并3列,合并2行 //1行 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); //第1单元格 cell.SetText("f"); cell = m_Row.CreateCell(); //从第2单元格开始合并 cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); // ctPr.gridSpan.val = "3";//合并3列 ctPr.AddNewVMerge().val = ST_Merge.restart; //开始合并行 ctPr.AddNewVAlign().val = ST_VerticalJc.center; //垂直居中 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cttc.GetPList()[0].AddNewR().AddNewT().Value = "777"; //2行 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); //第1单元格 cell.SetText("g"); cell = m_Row.CreateCell(); //第2单元格 cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); // ctPr.gridSpan.val = "3";//合并3列 ctPr.AddNewVMerge().val = ST_Merge.@continue;//继续合并行 //表插入图片 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell();//第1单元格 //inline方式插入图片 //gp = table.GetRow(table.Rows.Count - 1).GetCell(0).GetParagraph(table.GetRow(table.Rows.Count - 1).GetCell(0).GetCTTc().GetPList()[0]);//获得指定表单元格的段 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read); //读取图片文件 gr.AddPicture(gfs, (int)PictureType.PNG, "1.jpg", 500000, 500000); //插入图片 gfs.Close(); //Anchor方式插入图片 CT_Anchor an = new CT_Anchor(); an.distB = (uint)(0); an.distL = 114300u; an.distR = 114300U; an.distT = 0U; an.relativeHeight = 251658240u; an.behindDoc = false; //"0" an.locked = false; //"0" an.layoutInCell = true; //"1" an.allowOverlap = true; //"1" NPOI.OpenXmlFormats.Dml.CT_Point2D simplePos = new NPOI.OpenXmlFormats.Dml.CT_Point2D(); simplePos.x = (long)0; simplePos.y = (long)0; CT_EffectExtent effectExtent = new CT_EffectExtent(); effectExtent.b = 0L; effectExtent.l = 0L; effectExtent.r = 0L; effectExtent.t = 0L; //wrapSquare(四周) cell = m_Row.CreateCell(); //第2单元格 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run CT_WrapSquare wrapSquare = new CT_WrapSquare(); wrapSquare.wrapText = ST_WrapText.bothSides; gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件 // gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); //wrapTight(紧密) cell = m_Row.CreateCell(); //第3单元格 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run CT_WrapTight wrapTight = new CT_WrapTight(); wrapTight.wrapText = ST_WrapText.bothSides; wrapTight.wrapPolygon = new CT_WrapPath(); wrapTight.wrapPolygon.edited = false; wrapTight.wrapPolygon.start = new CT_Point2D(); wrapTight.wrapPolygon.start.x = 0; wrapTight.wrapPolygon.start.y = 0; CT_Point2D lineTo = new CT_Point2D(); wrapTight.wrapPolygon.lineTo = new List <CT_Point2D>(); lineTo = new CT_Point2D(); lineTo.x = 0; lineTo.y = 1343; wrapTight.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 1343; wrapTight.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 0; wrapTight.wrapPolygon.lineTo.Add(lineTo); lineTo.x = 0; lineTo.y = 0; wrapTight.wrapPolygon.lineTo.Add(lineTo); gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件 //gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapTight, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); //wrapThrough(穿越) cell = m_Row.CreateCell(); //第4单元格 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read); //读取图片文件 CT_WrapThrough wrapThrough = new CT_WrapThrough(); wrapThrough.wrapText = ST_WrapText.bothSides; wrapThrough.wrapPolygon = new CT_WrapPath(); wrapThrough.wrapPolygon.edited = false; wrapThrough.wrapPolygon.start = new CT_Point2D(); wrapThrough.wrapPolygon.start.x = 0; wrapThrough.wrapPolygon.start.y = 0; lineTo = new CT_Point2D(); wrapThrough.wrapPolygon.lineTo = new List <CT_Point2D>(); lineTo = new CT_Point2D(); lineTo.x = 0; lineTo.y = 1343; wrapThrough.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 1343; wrapThrough.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 0; wrapThrough.wrapPolygon.lineTo.Add(lineTo); lineTo.x = 0; lineTo.y = 0; wrapThrough.wrapPolygon.lineTo.Add(lineTo); // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 0, 0, wrapThrough, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); gp = m_Docx.CreateParagraph(); //gp.GetCTPPr().AddNewJc().val = ST_Jc.both; // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符 gp.IndentationFirstLine = 15; gr = gp.CreateRun(); gr.SetText("NPOI是POI项目的.NET版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。使用NPOI你就可以在没有安装Office或者相应环境的机器上对WORD/EXCEL文档进行读写。NPOI是构建在POI3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。"); gp = m_Docx.CreateParagraph(); // gp.GetCTPPr().AddNewJc().val = ST_Jc.both; //gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符 gp.IndentationFirstLine = 15; gr = gp.CreateRun(); gr.SetText("NPOI之所以强大,并不是因为它支持导出Excel,而是因为它支持导入Excel,并能“理解”OLE2文档结构,这也是其他一些Excel读写库比较弱的方面。通常,读入并理解结构远比导出来得复杂,因为导入你必须假设一切情况都是可能的,而生成你只要保证满足你自己需求就可以了,如果把导入需求和生成需求比做两个集合,那么生成需求通常都是导入需求的子集。"); //在本段中插图-wrapSquare //gr = gp.CreateRun();//创建run wrapSquare = new CT_WrapSquare(); wrapSquare.wrapText = ST_WrapText.bothSides; gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件 // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 900000, 200000, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); m_Docx.Write(ms); ms.Flush(); SaveToFile(ms, Path.GetPathRoot(Directory.GetCurrentDirectory()) + "\\NPOI.docx"); }