一个项目,要做一个从数据库读取数据,然后导出到word,因为涉及到后台数据库的读取,决定用npoi来导出word。
NPOI源码地址:http://npoi.codeplex.com/
NPOI 2.0 api文档: http://www.npoi.info/npoi2tutorial
因为npoi操作word的文章比较少,在由于版本不同,相关的函数可能不一样,这个就需要大家自己去慢慢的探索了。
例如:作者的api文档中
c.字体加粗
r1.SetBold(true);
实际我在调用时,调用的接口是 r1c1.IsBold = 12;
我使用的版本是:NPOI v2.2.0.0
需要引用的命名空间如下
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
最终效果图如下:
关键代码如下:
-
-
-
-
- protected void btnPrint_Click(object sender, EventArgs e)
- {
-
- XWPFDocument doc = new XWPFDocument();
-
- XWPFParagraph p1 = doc.CreateParagraph();
- p1.Alignment = ParagraphAlignment.CENTER;
-
-
-
-
-
- XWPFRun runTitle = p1.CreateRun();
- runTitle.IsBold = true;
- runTitle.SetText("军检验收单");
- runTitle.FontSize = 16;
- runTitle.SetFontFamily("宋体", FontCharRange.None);
-
- XWPFParagraph p2 = doc.CreateParagraph();
- XWPFRun run1 = p2.CreateRun();
- run1.SetText(" 军检项目号:");
- run1.FontSize = 12;
- run1.SetFontFamily("华文楷体", FontCharRange.None);
-
- #region 头部(6 rows)
-
-
- XWPFTable tableTop = doc.CreateTable(6, 5);
- tableTop.Width = 1000 * 5;
- tableTop.SetColumnWidth(0, 1300);
- tableTop.SetColumnWidth(1, 500);
- tableTop.SetColumnWidth(2, 1000);
- tableTop.SetColumnWidth(3, 500);
- tableTop.SetColumnWidth(4, 1700);
-
-
- tableTop.GetRow(0).MergeCells(1, 4);
- tableTop.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, tableTop, "产品名称"));
- tableTop.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc, tableTop, " "));
-
- tableTop.GetRow(1).MergeCells(1, 4);
- tableTop.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc, tableTop, "项目名称"));
- tableTop.GetRow(1).GetCell(1).SetParagraph(SetCellText(doc, tableTop, " "));
-
- tableTop.GetRow(2).MergeCells(1, 4);
- tableTop.GetRow(2).GetCell(0).SetParagraph(SetCellText(doc, tableTop, "施工依据", ParagraphAlignment.CENTER, 45));
- tableTop.GetRow(2).GetCell(1).SetParagraph(SetCellText(doc, tableTop, " ", ParagraphAlignment.CENTER, 45));
-
- tableTop.GetRow(3).GetCell(0).SetParagraph(SetCellText(doc, tableTop, "检验方式"));
- tableTop.GetRow(3).GetCell(1).SetParagraph(SetCellText(doc, tableTop, "独立检验"));
- tableTop.GetRow(3).GetCell(2).SetParagraph(SetCellText(doc, tableTop, " "));
- tableTop.GetRow(3).GetCell(3).SetParagraph(SetCellText(doc, tableTop, "联合检验"));
- tableTop.GetRow(3).GetCell(4).SetParagraph(SetCellText(doc, tableTop, " "));
-
- tableTop.GetRow(4).MergeCells(3, 4);
- tableTop.GetRow(4).GetCell(0).SetParagraph(SetCellText(doc, tableTop, "设备名称及编号"));
- tableTop.GetRow(4).GetCell(1).SetParagraph(SetCellText(doc, tableTop, " "));
- tableTop.GetRow(4).GetCell(2).SetParagraph(SetCellText(doc, tableTop, "设备制造厂"));
- tableTop.GetRow(4).GetCell(3).SetParagraph(SetCellText(doc, tableTop, " "));
-
-
- tableTop.GetRow(5).MergeCells(0, 4);
- CT_P para = new CT_P();
- XWPFParagraph pCell = new XWPFParagraph(para, tableTop.Body);
- pCell.Alignment = ParagraphAlignment.LEFT;
-
- XWPFRun r1c1 = pCell.CreateRun();
- r1c1.SetText("检验要素共9项");
- r1c1.FontSize = 12;
- r1c1.SetFontFamily("华文楷体", FontCharRange.None);
- tableTop.GetRow(5).GetCell(0).SetParagraph(pCell);
-
-
-
-
-
-
-
- #endregion
-
- #region 检验要素列表部分(数据库读取循环显示)
-
-
- XWPFTable tableContent = doc.CreateTable(45, 5);
- tableContent.Width = 1000 * 5;
- tableContent.SetColumnWidth(0, 300);
- tableContent.SetColumnWidth(1, 1000);
- tableContent.SetColumnWidth(2, 1000);
- tableContent.SetColumnWidth(3, 1000);
- tableContent.SetColumnWidth(4, 1700);
-
- tableContent.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, tableContent, "序号"));
- tableContent.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc, tableContent, "检验要素"));
- tableContent.GetRow(0).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "指标要求"));
- tableContent.GetRow(0).GetCell(3).SetParagraph(SetCellText(doc, tableContent, "实测值"));
- tableContent.GetRow(0).GetCell(4).SetParagraph(SetCellText(doc, tableContent, "测量工具编号及有效期"));
-
- for (int i = 1; i < 45; i++)
- {
- tableContent.GetRow(i).GetCell(0).SetParagraph(SetCellText(doc, tableContent, i.ToString(), ParagraphAlignment.CENTER, 50));
- tableContent.GetRow(i).GetCell(1).SetParagraph(SetCellText(doc, tableContent, "检验要素", ParagraphAlignment.CENTER, 50));
- tableContent.GetRow(i).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "指标要求", ParagraphAlignment.CENTER, 50));
- tableContent.GetRow(i).GetCell(3).SetParagraph(SetCellText(doc, tableContent, "实测值", ParagraphAlignment.CENTER, 50));
- tableContent.GetRow(i).GetCell(4).SetParagraph(SetCellText(doc, tableContent, "测量工具编号及有效期", ParagraphAlignment.CENTER, 50));
- }
-
- #endregion
-
- #region 底部内容
-
- XWPFTable tableBottom = doc.CreateTable(5, 4);
- tableBottom.Width = 1000 * 5;
-
- tableBottom.SetColumnWidth(0, 1000);
- tableBottom.SetColumnWidth(1, 1500);
- tableBottom.SetColumnWidth(2, 1000);
- tableBottom.SetColumnWidth(3, 1500);
-
- tableBottom.GetRow(0).MergeCells(0, 3);
- tableBottom.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, tableBottom, "附件:", ParagraphAlignment.LEFT, 80));
- tableBottom.GetRow(0).Height = 30;
-
- tableBottom.GetRow(1).MergeCells(0, 3);
- tableBottom.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc, tableBottom, "检验结论:", ParagraphAlignment.LEFT, 80));
- tableBottom.GetRow(1).Height = 30;
-
-
- tableBottom.GetRow(2).GetCell(0).SetParagraph(SetCellText(doc, tableBottom, "施工部门"));
- tableBottom.GetRow(2).GetCell(1).SetParagraph(SetCellText(doc, tableBottom, " "));
- tableBottom.GetRow(2).GetCell(2).SetParagraph(SetCellText(doc, tableBottom, "报验日期"));
- tableBottom.GetRow(2).GetCell(3).SetParagraph(SetCellText(doc, tableBottom, " "));
-
- tableBottom.GetRow(3).GetCell(0).SetParagraph(SetCellText(doc, tableBottom, "军检次数"));
- tableBottom.GetRow(3).GetCell(1).SetParagraph(SetCellText(doc, tableBottom, " "));
- tableBottom.GetRow(3).GetCell(2).SetParagraph(SetCellText(doc, tableBottom, "军检日期"));
- tableBottom.GetRow(3).GetCell(3).SetParagraph(SetCellText(doc, tableBottom, " "));
-
- tableBottom.GetRow(4).GetCell(0).SetParagraph(SetCellText(doc, tableBottom, "检验员"));
- tableBottom.GetRow(4).GetCell(1).SetParagraph(SetCellText(doc, tableBottom, " "));
- tableBottom.GetRow(4).GetCell(2).SetParagraph(SetCellText(doc, tableBottom, "军代表"));
- tableBottom.GetRow(4).GetCell(3).SetParagraph(SetCellText(doc, tableBottom, " "));
-
- #endregion
-
-
-
-
-
-
-
-
-
-
-
-
-
- #region 保存导出WebForm
-
-
-
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- doc.Write(ms);
- Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.doc", HttpUtility.UrlEncode("文件名" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8)));
- Response.BinaryWrite(ms.ToArray());
- Response.End();
-
- ms.Close();
- ms.Dispose();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
- }
- public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)
- {
-
- CT_P para = new CT_P();
- XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
- pCell.Alignment = ParagraphAlignment.CENTER;
- pCell.VerticalAlignment = TextAlignment.CENTER;
-
- XWPFRun r1c1 = pCell.CreateRun();
- r1c1.SetText(setText);
- r1c1.FontSize = 12;
- r1c1.SetFontFamily("华文楷体", FontCharRange.None);
-
-
- return pCell;
- }
-
- public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align, int textPos)
- {
- CT_P para = new CT_P();
- XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
-
- pCell.Alignment = align;
-
- XWPFRun r1c1 = pCell.CreateRun();
- r1c1.SetText(setText);
- r1c1.FontSize = 12;
- r1c1.SetFontFamily("华文楷体", FontCharRange.None);
- r1c1.SetTextPosition(textPos);
-
- return pCell;
- }
设置table的宽度,以及没列的宽度代码如下:
- XWPFTable tableTop = doc.CreateTable(6, 5);
- tableTop.Width = 1000 * 5;
- tableTop.SetColumnWidth(0, 1300);
- tableTop.SetColumnWidth(1, 500);
- tableTop.SetColumnWidth(2, 1000);
- tableTop.SetColumnWidth(3, 500);
- tableTop.SetColumnWidth(4, 1700);
和并列以及设置列的高度:
tableTop.GetRow(0).MergeCells(1, 4);/* 合并列 */
XWPFRun r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = 12;
r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
r1c1.SetTextPosition(textPos);//设置高度
NPOI已出现一段时间了,目前版本2.0 Beta 2 [v2.0.5],网上关于NPOI操作xlsx文章较多,而关于docx的几乎没有,尽管NPOI对于Word还不稳定,经过一阵捣鼓后终于实现了表的简单操作:创建表、创建行、创建单元,单元行和列的合并。
环境:vs2010,netframework4
- private void button1_Click(object sender, EventArgs e)
- {
- MemoryStream ms = new MemoryStream();
- XWPFDocument m_Docx = new XWPFDocument();
- m_Docx = CreatDocxTable();
- m_Docx.Write(ms);
- ms.Flush();
- SaveToFile(ms,"d:\\test.docx");
- }
- protected XWPFDocument CreatDocxTable()
- {
- XWPFDocument m_Docx = new XWPFDocument();
- XWPFParagraph p0 = m_Docx.CreateParagraph();
- XWPFRun r0 = p0.CreateRun();
- r0.SetText("DOCX表");
-
- XWPFTable table = m_Docx.CreateTable(1, 3);
- table.GetRow(0).GetCell(0).SetText("111");
- table.GetRow(0).GetCell(1).SetText("222");
- table.GetRow(0).GetCell(2).SetText("333");
-
- XWPFTableRow m_Row = table.CreateRow();
- m_Row = table.CreateRow();
- m_Row.GetCell(0).SetText("211");
-
-
- m_Row = table.InsertNewTableRow(0);
- XWPFTableCell cell = m_Row.CreateCell();
- CT_Tc cttc = cell.GetCTTc();
- CT_TcPr ctPr = cttc.AddNewTcPr();
- ctPr.gridSpan.val = "3";
- cttc.GetPList()[0].AddNewPPr().AddNewJc().val= ST_Jc.center;
- cttc.GetPList()[0].AddNewR().AddNewT().Value = "abc";
-
- XWPFTableRow td3 = table.InsertNewTableRow(table.Rows.Count - 1);
- cell = td3.CreateCell();
- cttc = cell.GetCTTc();
- ctPr = cttc.AddNewTcPr();
- ctPr.gridSpan.val = "3";
- cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
- cttc.GetPList()[0].AddNewR().AddNewT().Value = "qqq";
-
-
- CT_Row 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.gridSpan.val = "3";
- cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
- cttc.GetPList()[0].AddNewR().AddNewT().Value = "sss";
-
-
-
- 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.gridSpan.val = "2";
- 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 = "xxx";
- cell = m_Row.CreateCell();
- cell.SetText("ddd");
-
- 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.gridSpan.val = "2";
- ctPr.AddNewVMerge().val = ST_Merge.@continue;
- cell = m_Row.CreateCell();
- cell.SetText("kkk");
-
-
-
-
-
-
-
-
-
-
-
-
- return m_Docx;
- }
- static void SaveToFile(MemoryStream ms, string fileName)
- {
- using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- byte[] data = ms.ToArray();
-
- fs.Write(data, 0, data.Length);
- fs.Flush();
- data = null;
- }
- }