//地政函档案封面及收件回执 public static void DZHDAFMJSJHZ(Dictionary <string, string> dic, string tplFilePath, string outputPrintFilePath) { using (FileStream tplFileStream = new FileStream(tplFilePath, FileMode.Open, FileAccess.Read)) using (FileStream outputFileStream = File.Create(outputPrintFilePath)) { List <RespDZHSJHZ> list = new DbHelper().QueryRespDZHSJHZ(SharpDbPrinter.Program.globalYwid); XWPFDocument tplDoc = new XWPFDocument(tplFileStream); var printDoc = DocxBaseRelace(tplDoc, dic); var firstTable = printDoc.Tables[0]; for (int i = 0; i < list.Count; i++) { XWPFTableRow newRow = new XWPFTableRow(new NPOI.OpenXmlFormats.Wordprocessing.CT_Row(), firstTable); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.MergeCells(0, 1); if (list[i].MTR_NAME == null && list[i].OLD_NUM == null && list[i].DUPL_NUM == null) { } else { newRow.GetCell(0).SetParagraph(SetCellText(printDoc, firstTable, list[i].MTR_NAME ?? "0")); newRow.GetCell(1).SetParagraph(SetCellText(printDoc, firstTable, list[i].OLD_NUM ?? "0")); newRow.GetCell(2).SetParagraph(SetCellText(printDoc, firstTable, list[i].DUPL_NUM ?? "0")); } firstTable.AddRow(newRow, 2 + i); } firstTable.RemoveRow(1); printDoc.Write(outputFileStream); } }
void CreateExceptionRecordsCell(ReportDownloadModel Datas, XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { DealWithMergeCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); StartMergeRow(cttc, ctPr, "异常记录"); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "检测类型"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "测点编号"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "测点位置"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "异常次数"); SetAlign(cell); for (int i = 1; i <= Datas.ExceptionRecordNumber; i++) { m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行 //横向创建4个单元格 for (int k = 1; k < 5; k++) { cell = m_Row.CreateCell(); SetAlign(cell); } } }
//创建评估报告索力评估结果单元格 void CreateCableForceAssessmentResultCell(XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { DealWithMergeCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行 cell = m_Row.CreateCell(); SetBoldFontCell(cell, "吊杆及系杆索力评估"); SetAlign(cell); for (int i = 1; i < 4; i++) { cell = m_Row.CreateCell(); SetAlign(cell); } }
/// <summary> /// 应用表格模板 /// </summary> /// <param name="tableContent"></param> /// <param name="tableTemplate"></param> /// <param name="colNames"></param> private void ApplyToTableTemplate(XWPFTable tableContent, TableTemplateModel tableTemplate, Dictionary <int, string> colNames) { int startRowNum = tableTemplate.StartRowNumber; for (var rowIndex = 0; rowIndex < tableTemplate.Value.Rows.Count; rowIndex++) { int documentRowIndex = rowIndex + startRowNum; XWPFTableRow row = rowIndex == 0 ? tableContent.GetRow(documentRowIndex) : tableContent.InsertNewTableRow(documentRowIndex - 1); int cellCount = row.GetTableCells().Count; int count = colNames.Max(m => m.Key); for (int i = cellCount - 1; i < count; i++) { row.CreateCell(); } foreach (KeyValuePair <int, string> colName in colNames) { string[] colValues = colName.Value.Split(','); string value = tableTemplate.Value.Rows[rowIndex][colValues[0]].ToString(); XWPFParagraph paragraph = GetCellContent(rowIndex, colName.Key, tableContent, value, tableTemplate.OnSetCellText); XWPFTableCell cell = row.GetCell(colName.Key); cell.SetParagraph(paragraph); for (var i = 1; i < colValues.Length; i++) { ApplyCommand(colValues[i], colName.Key, row); } } } }
//创建“评估结果”表格表头部分 void CreateAssessmentResultHeaderAndSetValue(XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { DealWithMergeCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); StartMergeRow(cttc, ctPr, "评估结果"); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "评估项目"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "评估结论"); SetAlign(cell); cell = m_Row.CreateCell(); cell.SetText(""); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "建议"); SetAlign(cell); }
/// <summary> /// 对合并单元格进行前处理 /// </summary> /// <param name="table"></param> /// <param name="m_NewRow"></param> /// <param name="m_Row"></param> /// <param name="cell"></param> /// <param name="cttc"></param> /// <param name="ctPr"></param> static void DealWithMergeCell(XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); }
/// <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 static void ExportDocumentWithDataTable(XWPFDocument docx, DocumentSetting setting, WordTable wordTable) { if (wordTable == null || wordTable.Rows == null || wordTable.Rows.Count == 0) { return; } CT_P p = docx.Document.body.AddNewP(); p.AddNewPPr().AddNewJc().val = ST_Jc.center; var Rows = wordTable.Rows; XWPFTable table = docx.CreateTable(1, wordTable.ColumnCount); table.RemoveRow(0);//remove first blank row table.Width = wordTable.Width; for (var i = 0; i < Rows.Count; i++) { var row = Rows[i]; if (row == null || row.Count == 0) { continue; } CT_Row nr = new CT_Row(); XWPFTableRow mr = new XWPFTableRow(nr, table); table.AddRow(mr); for (var j = 0; j < row.Count; j++) { var cell = row[j]; var c1 = mr.CreateCell(); var ct = c1.GetCTTc(); var cp = ct.AddNewTcPr(); cp.gridSpan = new CT_DecimalNumber(); cp.gridSpan.val = Convert.ToString(cell.MergeColumnNumber); var tblW = cp.AddNewTcW(); tblW.type = ST_TblWidth.dxa; tblW.w = cell.Width.ToString(); c1.SetParagraph(SetCellText(docx, table, cell)); c1.SetBorderTop(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF"); c1.SetBorderRight(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF"); c1.SetBorderLeft(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF"); c1.SetBorderBottom(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF"); } } }
public void getRenderedCell(Marker input, StyleConfig config, XWPFTableRow parentRow) { StyleConfig markerStyle = (StyleConfig)config.Clone(); XWPFTableCell tableCellContainer = parentRow.CreateCell(); XWPFParagraph cellContents; switch (input) { case THMarker tHMarker: markerStyle.isBold = true; cellContents = tableCellContainer.AddParagraph(markerStyle); cellContents.SetBidi(configDocx.rightToLeft); foreach (Marker marker in input.Contents) { RenderMarker(marker, markerStyle, cellContents); } break; case THRMarker tHRMarker: markerStyle.isAlignRight = true; markerStyle.isBold = true; cellContents = tableCellContainer.AddParagraph(markerStyle); cellContents.SetBidi(configDocx.rightToLeft); foreach (Marker marker in input.Contents) { RenderMarker(marker, markerStyle, cellContents); } break; case TCMarker tCMarker: cellContents = tableCellContainer.AddParagraph(markerStyle); cellContents.SetBidi(configDocx.rightToLeft); foreach (Marker marker in input.Contents) { RenderMarker(marker, markerStyle, cellContents); } break; case TCRMarker tCRMarker: markerStyle.isAlignRight = true; cellContents = tableCellContainer.AddParagraph(markerStyle); cellContents.SetBidi(configDocx.rightToLeft); foreach (Marker marker in input.Contents) { RenderMarker(marker, markerStyle, cellContents); } break; default: break; } }
private XWPFDocument InsertTable(XWPFDocument doc, Table t) { var maxColCount = t.Rows.Max(x => x.Cells.Count); if (t == null) { return(doc); } var table = doc.CreateTable(); table.Width = t.Width; int index = 0; t.Rows?.ForEach(r => { XWPFTableRow tableRow = index == 0 ? table.GetRow(0) : table.CreateRow(); for (int i = 0; i < r.Cells.Count; i++) { var cell = r.Cells[i]; var xwpfCell = i == 0 ? tableRow.GetCell(0) : tableRow.AddNewTableCell(); foreach (var para in cell.Paragraphs) { xwpfCell.AddParagraph().Set(para); } if (!string.IsNullOrWhiteSpace(cell.Color)) { tableRow.GetCell(i).SetColor(cell.Color); } } //补全单元格,并合并 var rowColsCount = tableRow.GetTableICells().Count; if (rowColsCount < maxColCount) { for (int i = rowColsCount - 1; i < maxColCount; i++) { tableRow.CreateCell(); } tableRow.MergeCells(rowColsCount - 1, maxColCount); } index++; }); return(doc); }
private static void CopyRow(XWPFTableRow templateRow, XWPFTableRow row) { if (row.Height != templateRow.Height) { row.Height = templateRow.Height; } List <XWPFTableCell> templateCells = templateRow.GetTableCells(); foreach (XWPFTableCell templateCell in templateCells) { XWPFTableCell cell = row.CreateCell(); WordGenerator.CopyCell(templateCell, cell); } }
private static void DoAddTable(XWPFTable table, XWPFParagraph paragraph, AddTableOptions TableOptions) { string runText; IList <XWPFRun> listRun = paragraph.Runs; if (TableOptions != null && TableOptions.dataTable != null && TableOptions.dataTable.Rows.Count > 0) { var nCls = TableOptions.dataTable.Columns.Count; var nRows = TableOptions.dataTable.Rows.Count; for (int i = 0; i < listRun.Count; i++) { XWPFRun run = listRun[i]; runText = run.Text; if (run.Text == TableOptions.PlaceHolder.ToString()) { paragraph.RemoveRun(i); int j = 0; foreach (DataColumn c in TableOptions.dataTable.Columns) { //table.GetRow(0).GetCell(j).SetColor("#AEAAAA"); table.GetRow(0).GetCell(j).SetText(c.ColumnName); //XWPFParagraph pIO = table.GetRow(0).GetCell(j).AddParagraph(); //XWPFRun rIO = pIO.CreateRun(); //rIO.FontFamily = "宋体"; //rIO.FontSize = 11; //rIO.IsBold = true; //rIO.SetText(c.ColumnName); j++; } j = 0; for (int n = 0; n < nRows; n++) { var dr = TableOptions.dataTable.Rows[n]; CT_Row nr = new CT_Row(); XWPFTableRow mr = new XWPFTableRow(nr, table);//创建行 //mr.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1000;//设置行高 //nr.AddNewTrPr().AddNewTrHeight().val = (ulong)1000;//设置行高(这两行都得有) table.AddRow(mr);//将行添加到table中 for (int m = 0; m < nCls; m++) { mr.CreateCell().SetText(dr[m].ToString()); } } } } } }
public static XWPFTableCell GetCell(XWPFTableRow row, int line, XWPFTableRow modelRow) { XWPFTableCell cell = row.GetCell(line); if (cell == null) { cell = row.CreateCell(); if (modelRow != null) { var modelcell = modelRow.GetCell(line); cell.SetVerticalAlignment(modelcell.GetVerticalAlignment()); cell.SetColor(modelcell.GetColor()); } } return(cell); }
/// <summary> /// 应用命令 /// </summary> /// <param name="command"></param> /// <param name="colIndex"></param> /// <param name="row"></param> private void ApplyCommand(string command, int colIndex, XWPFTableRow row) { string[] commands = command.Split(':'); if (commands.Length != 2) { return; } string type = commands[0]; string typeValue = commands[1]; if (type == "RowSpan") { int endColIndex = colIndex + typeValue.ConvertTo <int>() - 1; int cellCount = row.GetTableCells().Count; for (int j = cellCount; j <= endColIndex; j++) { row.CreateCell(); } row.MergeCells(colIndex, endColIndex); } }
// word 添加表格, 表格的行宽怎么确定 public static void AddTable(XWPFDocument doc, DataTable dt) { // XWPFDocument doc = new XWPFDocument(); //创建表格-提前创建好表格后填数 XWPFTable table = doc.CreateTable();//4行5列 int rowCount = dt.Rows.Count + 1; int colCount = dt.Columns.Count; XWPFTableRow titles = table.GetRow(0); // table.AddRow(titles); for (int i = 0; i < colCount; i++) { string title = dt.Columns[i].ColumnName; XWPFTableCell cell = titles.GetCell(i); if (cell == null) { cell = titles.CreateCell(); } // cell.SetParagraph(SetCellText(doc, table, title)); cell.SetText(title); } for (int i = 1; i < rowCount; i++)//有3个数组 { DataRow dr = dt.Rows[i - 1]; XWPFTableRow tablerow = table.CreateRow(); // XWPFTableRow tablerow = table.GetRow(i); for (int j = 0; j < colCount; j++) { string content = dr[j].ToString(); // tablerow.GetCell(j).SetParagraph(SetCellText(doc, table, content)); tablerow.GetCell(j).SetText(content); } // table.AddRow(tablerow); } }
// word 给表格追加行, 下标对应 public static void AppendTable(XWPFTable table, DataTable newDatas) { //创建表格-提前创建好表格后填数 int rowCount = newDatas.Rows.Count; int colCount = newDatas.Columns.Count; for (int i = 0; i < rowCount; i++) { XWPFTableRow row = table.CreateRow(); for (int j = 0; j < colCount; j++) { XWPFTableCell cell = row.GetCell(j); if (cell == null) { cell = row.CreateCell(); } string content = newDatas.Rows[i][j]?.ToString(); cell.SetText(content); } } }
public static void CreateFileByTemplate(List <TaskResultModel> resultList, string tempFile, string saveFile, string taskId) { using (FileStream stream = File.OpenRead(tempFile)) { XWPFDocument doc = new XWPFDocument(stream); //遍历段落 foreach (var para in doc.Paragraphs) { } //遍历表格 List <string> picFormat = new List <string>(); picFormat.Add(".jpeg"); picFormat.Add(".jpg"); picFormat.Add(".png"); picFormat.Add(".bmp"); picFormat.Add(".tiff"); picFormat.Add(".svg"); picFormat.Add(".gif"); //picFormat.Add(""); for (int index = 0; index < doc.Tables.Count; index++) { XWPFTable table = doc.Tables[index]; if (index == 2) { int num = table.Rows.Count - 3 - resultList.Count;//总行数-文字描述的行数-测试项目的数量 = 要删除,或者增加的单元格行数 if (num >= 0) { for (int i = 0; i < num; i++) { table.RemoveRow(2 + resultList.Count); } } else//如何测试项目大于当前文档已有的行数,需要进行添加行的操作 { for (int i = num; i < 0; i++) { NPOI.OpenXmlFormats.Wordprocessing.CT_Row nr = new NPOI.OpenXmlFormats.Wordprocessing.CT_Row(); XWPFTableRow mr = new XWPFTableRow(nr, table);//创建行 //mr.AddNewTableCell(); //mr.AddNewTableCell(); //mr.AddNewTableCell(); //mr.AddNewTableCell(); //mr.AddNewTableCell(); //mr.AddNewTableCell(); mr.CreateCell(); mr.CreateCell(); mr.CreateCell(); mr.CreateCell(); mr.CreateCell(); mr.CreateCell(); table.AddRow(mr, table.Rows.Count - 1); //XWPFTableRow newRow = table.GetRow(2); //for (int j = 0; j < newRow.GetTableCells().Count; j++) //{ // XWPFParagraph par = doc.CreateParagraph(); // XWPFRun run = par.CreateRun(); // run.SetText(""); // newRow.GetCell(j).SetParagraph(par); //} //table.AddRow(newRow, table.Rows.Count - 1); } } int item = 0; for (int i = 0; i < table.Rows.Count; i++) { if (i >= 2 && i < table.Rows.Count - 1) { List <XWPFTableCell> columnsList = table.Rows[i].GetTableCells(); int columns = columnsList.Count; for (int col = 0; col < columns; col++) { if (col == 0) //序号 列 { if (columnsList[col].GetText() != "") { columnsList[col].Paragraphs[0].RemoveRun(0); } columnsList[col].SetText((item + 1).ToString()); string test = columnsList[col].GetText(); //XWPFParagraph par = doc.CreateParagraph(); //XWPFRun run = par.CreateRun(); //run.SetText((item + 1).ToString()); //table.Rows[i].GetCell(col).SetParagraph(par); } else if (col == 1) //检验项目 { //XWPFParagraph par = doc.CreateParagraph(); //XWPFRun run = par.CreateRun(); //run.SetText(resultList[item].indicatorName); //table.Rows[i].GetCell(col).SetParagraph(par); columnsList[col].SetText(resultList[item].indicatorName); } else if (col == 2)//技术标准要求 { //XWPFParagraph par = doc.CreateParagraph(); //XWPFRun run = par.CreateRun(); //run.SetText(resultList[item].indicatorDesc); //table.Rows[i].GetCell(col).SetParagraph(par); columnsList[col].SetText(resultList[item].indicatorDesc); } else if (col == 3) { //XWPFParagraph par = doc.CreateParagraph(); //XWPFRun run = par.CreateRun(); //run.SetText(resultList[item].indicatorId.ToString()); //table.Rows[i].GetCell(col).SetParagraph(par); columnsList[col].SetText(resultList[item].indicatorId.ToString()); } else if (col == 4) { //XWPFParagraph par = doc.CreateParagraph(); //XWPFRun run = par.CreateRun(); //run.SetText(resultList[item].taskRecord); //table.Rows[i].GetCell(col).SetParagraph(par); columnsList[col].SetText(resultList[item].taskRecord); } else if (col == 5) { //XWPFParagraph par = doc.CreateParagraph(); //XWPFRun run = par.CreateRun(); if (resultList[item].taskResult == 2) { //run.SetText("合格"); columnsList[col].SetText("合格"); } else { //run.SetText("不合格"); columnsList[col].SetText("不合格"); } //table.Rows[i].GetCell(col).SetParagraph(par); } } item++; } } } if (index == 3) { XWPFTableCell Cell = table.GetRow(table.Rows.Count - 1).GetCell(0);//获取到进行图片插入的单元格 XWPFParagraph p = Cell.AddParagraph(); XWPFRun run = p.CreateRun(); foreach (var item in resultList) { if (item.attachment != "") { string path = item.attachment; string[] picArr = path.Split(new char[] { ';' }); foreach (var str in picArr) { string strTask = string.Format("/{0}/", taskId); string fileName = str.Replace(strTask, ""); foreach (var picItem in picFormat) { //GetFTPFilePath(); if (str.Contains(picItem)) { GetFTPFilePath(str); string localFilePath = Application.StartupPath + @"\temp" + str.Replace("/", @"\"); System.Drawing.Image image = System.Drawing.Image.FromFile(localFilePath); using (FileStream img = new FileStream(localFilePath, FileMode.Open, FileAccess.Read)) { PictureType picType = PictureType.PNG; switch (picItem) { case ".png": picType = PictureType.PNG; break; case ".jpg": picType = PictureType.JPEG; break; case ".jpeg": picType = PictureType.JPEG; break; case ".bmp": picType = PictureType.BMP; break; default: break; } run.AddPicture(img, (int)picType, fileName, NPOI.Util.Units.ToEMU(image.Width), NPOI.Util.Units.ToEMU(image.Height)); } //img.Close(); } } } } //string localFilePath = Application.StartupPath + @"\temp" + remoteFilePath.Replace("/", @"\"); } } } string fileRename = string.Format("\\航科院安保设施实验室_测试报告{0}.docx", DateTime.Now.ToString("yyyyMMddHHmmss")); FileStream out1 = new FileStream(saveFile + fileRename, FileMode.Create, FileAccess.Write); doc.Write(out1); out1.Close(); } }
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); }
//地政函审批表 /// <summary> /// 将模板文档的源关键字替换成目标文本 /// </summary> /// <param name="dic">基础替换文本字典</param> /// <param name="formatParaDic">包含换行符的替换目标字典</param> /// <param name="tplFilePath">模板文件的路径</param> /// <param name="outputPrintFilePath">输出文件路径</param> public static void DZHSPB(Dictionary <string, string> dic, Dictionary <string, string> formatParaDic, string tplFilePath, string outputPrintFilePath) { using (FileStream tplFileStream = new FileStream(tplFilePath, FileMode.Open, FileAccess.Read)) using (FileStream outputFileStream = File.Create(outputPrintFilePath)) { List <RespDZHSPBChild> list = new DbHelper().QueryDZHSPB_Child(SharpDbPrinter.Program.globalYwid); XWPFDocument tplDoc = new XWPFDocument(tplFileStream); var printDoc = DocxRepalceWithParaFormat(tplDoc, formatParaDic); printDoc = DocxBaseRelace(printDoc, dic); int row1 = 9; var firstTable = printDoc.Tables[0]; for (int i = 0; i < list.Count; i++) { XWPFTableRow newRow = new XWPFTableRow(new NPOI.OpenXmlFormats.Wordprocessing.CT_Row(), firstTable); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.CreateCell(); newRow.MergeCells(5, 9); newRow.MergeCells(0, 4); newRow.GetCell(0).SetParagraph(SetCellText(printDoc, firstTable, list[i].th ?? "")); newRow.GetCell(1).SetParagraph(SetCellText(printDoc, firstTable, list[i].YTDSYZH ?? "")); newRow.GetCell(2).SetParagraph(SetCellText(printDoc, firstTable, list[i].PZMJ ?? "")); firstTable.AddRow(newRow, row1 + 1 + i); } firstTable.RemoveRow(row1); printDoc.Write(outputFileStream); } }
private void NPOITestFillData(string file) { #region 读取Word XWPFDocument doc; using (FileStream fileread = File.OpenRead(file)) { doc = new XWPFDocument(fileread); } #endregion List <string>[] data = new List <string> [3]; #region 组织填充数据 List <string> a = new List <string>(); List <string> b = new List <string>(); List <string> c = new List <string>(); a.Add("1.1"); a.Add("1.2"); a.Add("1.3"); a.Add("1.4"); a.Add("1.5"); a.Add("1.6"); a.Add("1.7"); b.Add("2.1"); b.Add("2.2"); b.Add("2.3"); b.Add("2.4"); b.Add("2.5"); b.Add("2.6"); c.Add("3.1"); c.Add("3.2"); c.Add("3.3"); c.Add("3.4"); c.Add("3.5"); c.Add("3.6"); c.Add("3.7"); c.Add("3.8"); c.Add("3.9"); data[0] = a; data[1] = b; data[2] = c; #endregion WordTable wt = new WordTable(data); wt.CaptionLineCount = 2; //标题行数 XWPFTable table = LocationTable(doc, "本年发生的非同一控制下企业合并情况"); if (wt.ObjectData != null) { for (int i = 0; i < wt.ObjectData.Length + wt.CaptionLineCount; i++) { if (i >= wt.CaptionLineCount) { XWPFTableRow row; string[] rowdata = wt.ObjectData[i - wt.CaptionLineCount].ToArray <string>(); if (i < table.Rows.Count) { row = table.GetRow(i); row.GetCTRow().AddNewTrPr().AddNewTrHeight().val = 397; row.GetCTRow().trPr.GetTrHeightArray(0); for (int n = 0; n < rowdata.Length; n++) { XWPFTableCell cell = row.GetCell(n); //模板中的单元格少时,接收数据将会部分丢失 //也可以在下边if后添加else在该行后补充单元格 //按接收数据循环,所以单元格数多于接收数据时不需要另做处理,该行后边的部分单元格无法补填充 if (cell != null) { //SetText是追加内容,所以要先删除单元格内容(删除单元格内所有段落)再写入 for (int p = 0; p < cell.Paragraphs.Count; p++) { cell.RemoveParagraph(p); } for (int t = 0; t < cell.Tables.Count; t++) { //表格删除 //cell.RemoveTable(t); } cell.SetText(rowdata[n]); } } } else { //添加新行 //row = table.InsertNewTableRow(table.Rows.Count - 1); row = new XWPFTableRow(new CT_Row(), table); row.GetCTRow().AddNewTrPr().AddNewTrHeight().val = 100; table.AddRow(row); for (int n = 0; n < rowdata.Length; n++) { XWPFTableCell cell = row.CreateCell(); CT_Tc tc = cell.GetCTTc(); CT_TcPr pr = tc.AddNewTcPr(); tc.GetPList()[0].AddNewR().AddNewT().Value = rowdata[n]; } } } } } #region 保存Word FileStream filewrite = new FileStream(file, FileMode.Create, FileAccess.Write); try { doc.Write(filewrite); } catch (Exception x) { lblStatus.Text = x.Message; } finally { filewrite.Close(); } #endregion }
/// <summary> /// 将行拷贝到目标表格 /// 只拷贝了基本的样式 /// </summary> /// <param name="targetTable">目标表格</param> /// <param name="sourceRow">源表格行</param> /// <param name="rowIndex">行索引</param> /// <param name="maxCol">表格最大列数</param> public static void CopyRowToTable(XWPFTable targetTable, XWPFTableRow sourceRow, int rowIndex, int maxCol) { //在表格指定位置新增一行 XWPFTableRow targetRow = rowIndex == 0 ? targetTable.GetRow(0) : targetTable.CreateRow(); //复制行属性 targetRow.GetCTRow().trPr = sourceRow.GetCTRow().trPr; List <XWPFTableCell> cellList = sourceRow.GetTableCells(); if (null == cellList) { return; } //复制列及其属性和内容 int colIndex = 0; foreach (XWPFTableCell sourceCell in cellList) { XWPFTableCell targetCell = null; //新增行会默认有一个单元格,因此直接获取就好 try { targetCell = targetRow.GetCell(colIndex); } catch (Exception) { } if (targetCell == null) { targetCell = targetRow.CreateCell(); } //列属性 targetCell.GetCTTc().tcPr = sourceCell.GetCTTc().tcPr; //段落属性 if (sourceCell.Paragraphs != null && sourceCell.Paragraphs.Count > 0) { var paragraph = targetCell.Paragraphs[0]; var ctp = (CT_P)typeof(XWPFParagraph).GetField("paragraph", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(paragraph); var sourceParagraph = sourceCell.Paragraphs[0]; var sourceCtp = (CT_P)typeof(XWPFParagraph).GetField("paragraph", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sourceParagraph); paragraph.Alignment = sourceParagraph.Alignment; ctp.pPr = sourceCtp.pPr; if (sourceCell.Paragraphs[0].Runs != null && sourceCell.Paragraphs[0].Runs.Count > 0) { XWPFRun cellR = targetCell.Paragraphs[0].CreateRun(); var srcRun = sourceCell.Paragraphs[0].Runs[0]; cellR.SetText(sourceCell.GetText()); cellR.IsBold = srcRun.IsBold; cellR.SetColor(srcRun.GetColor()); cellR.FontFamily = srcRun.FontFamily; cellR.IsCapitalized = srcRun.IsCapitalized; cellR.IsDoubleStrikeThrough = srcRun.IsDoubleStrikeThrough; cellR.IsEmbossed = srcRun.IsEmbossed; cellR.IsImprinted = srcRun.IsImprinted; cellR.IsItalic = srcRun.IsItalic; cellR.IsShadowed = srcRun.IsShadowed; } else { targetCell.SetText(sourceCell.GetText()); } } else { targetCell.SetText(sourceCell.GetText()); } colIndex++; } if (cellList.Count < maxCol) { try { targetRow.MergeCells(cellList.Count - 1, maxCol - 1); } catch { } } }
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"); }