예제 #1
0
        public void TestCreateRow()
        {
            XWPFDocument doc = new XWPFDocument();

            CT_Tbl table = new CT_Tbl();
            CT_Row r1    = table.AddNewTr();

            r1.AddNewTc().AddNewP();
            r1.AddNewTc().AddNewP();
            CT_Row r2 = table.AddNewTr();

            r2.AddNewTc().AddNewP();
            r2.AddNewTc().AddNewP();
            CT_Row r3 = table.AddNewTr();

            r3.AddNewTc().AddNewP();
            r3.AddNewTc().AddNewP();

            XWPFTable xtab = new XWPFTable(table, doc);

            Assert.AreEqual(3, xtab.GetNumberOfRows());
            Assert.IsNotNull(xtab.GetRow(2));

            //add a new row
            xtab.CreateRow();
            Assert.AreEqual(4, xtab.GetNumberOfRows());

            //check number of cols
            Assert.AreEqual(2, table.GetTrArray(0).SizeOfTcArray());

            //check creation of first row
            xtab = new XWPFTable(new CT_Tbl(), doc);
            Assert.AreEqual(1, xtab.GetCTTbl().GetTrArray(0).SizeOfTcArray());
        }
예제 #2
0
        /**
         * create a new XWPFTableCell and add it to the tableCell-list of this tableRow
         * @return the newly Created XWPFTableCell
         */
        public XWPFTableCell CreateCell()
        {
            XWPFTableCell tableCell = new XWPFTableCell(ctRow.AddNewTc(), this, table.Body);

            tableCells.Add(tableCell);
            return(tableCell);
        }
예제 #3
0
        public void TestGetText()
        {
            XWPFDocument doc       = new XWPFDocument();
            CT_Tbl       table     = new CT_Tbl();
            CT_Row       row       = table.AddNewTr();
            CT_Tc        cell      = row.AddNewTc();
            CT_P         paragraph = cell.AddNewP();
            CT_R         run       = paragraph.AddNewR();
            CT_Text      text      = run.AddNewT();

            text.Value = ("finally I can Write!");

            XWPFTable xtab = new XWPFTable(table, doc);

            Assert.AreEqual("finally I can Write!\n", xtab.GetText());
        }
예제 #4
0
        /// <summary>
        /// Table添加Row
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="columns">字段名</param>
        /// <param name="dt">数据源</param>
        protected virtual void DocTableAddTr(XWPFTable table, string[] columns, DataTable dt)
        {
            var lctr = table.Rows[table.Rows.Count - 1].GetCTRow();

            foreach (DataRow dr in dt.Rows)
            {
                var nctr = new CT_Row();
                for (int i = 0; i < columns.Length; i++)
                {
                    var text = dr[columns[i]].ToString();

                    var lctcpr = lctr.GetTcArray(i).tcPr;
                    var nctct  = nctr.AddNewTc();
                    if (lctcpr.gridSpan != null)
                    {
                        nctct.AddNewTcPr().AddNewGridspan().val = lctcpr.gridSpan.val;
                    }

                    nctct.AddNewP().AddNewR().AddNewT().Value = text;
                }
                table.AddRow(new XWPFTableRow(nctr, table));
            }
        }
예제 #5
0
        /// <summary>
        /// 导出分样单
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="list">对象集合</param>
        /// <param name="filetemplatepath">模板路径</param>
        /// <param name="path">生成文件路径</param>
        public static void RenderToWord <T>(List <T> list, string filetemplatepath, string path)
        {
            using (FileStream stream = System.IO.File.OpenRead(filetemplatepath))
            {
                XWPFDocument doc = new XWPFDocument(stream);
                //遍历表格
                var table = doc.Tables[0];

                int tableindex = 1;
                foreach (T data in list)
                {
                    //添加更多行
                    if (table.GetRow(tableindex) == null)
                    {
                        CT_Row row = new CT_Row();
                        row.AddNewTc();
                        row.AddNewTc();
                        row.AddNewTc();
                        row.AddNewTc();
                        row.AddNewTc();
                        row.AddNewTc();
                        row.AddNewTc();
                        table.AddRow(new XWPFTableRow(row, table));
                    }
                    //填充单元格数据
                    PropertyInfo[] ps = typeof(T).GetProperties();

                    string samplenum = "", samplename = "", checkprojectandstandard = "", storcondition = "", checkdepar = "", urgentlevel = "", remarks = "";
                    if (ps.First(p => p.Name == "samplenum").GetValue(data, null) != null)
                    {
                        samplenum = ps.First(p => p.Name == "samplenum").GetValue(data, null).ToString();
                    }
                    if (ps.First(p => p.Name == "samplename").GetValue(data, null) != null)
                    {
                        samplename = ps.First(p => p.Name == "samplename").GetValue(data, null).ToString();
                    }
                    if (ps.First(p => p.Name == "checkprojectandstandard").GetValue(data, null) != null)
                    {
                        checkprojectandstandard = ps.First(p => p.Name == "checkprojectandstandard").GetValue(data, null).ToString();
                    }
                    if (ps.First(p => p.Name == "storcondition").GetValue(data, null) != null)
                    {
                        storcondition = ps.First(p => p.Name == "storcondition").GetValue(data, null).ToString();
                    }
                    if (ps.First(p => p.Name == "checkdepar").GetValue(data, null) != null)
                    {
                        checkdepar = ps.First(p => p.Name == "checkdepar").GetValue(data, null).ToString();
                    }
                    if (ps.First(p => p.Name == "urgentlevel").GetValue(data, null) != null)
                    {
                        urgentlevel = ps.First(p => p.Name == "urgentlevel").GetValue(data, null).ToString();
                    }
                    if (ps.First(p => p.Name == "remarks").GetValue(data, null) != null)
                    {
                        remarks = ps.First(p => p.Name == "remarks").GetValue(data, null).ToString();
                    }

                    table.GetRow(tableindex).GetCell(0).SetText(samplenum);               //样品编号
                    table.GetRow(tableindex).GetCell(1).SetText(samplename);              //样品名称
                    table.GetRow(tableindex).GetCell(2).SetText(checkprojectandstandard); //检验项目及标准
                    table.GetRow(tableindex).GetCell(3).SetText(storcondition);           //存储条件
                    table.GetRow(tableindex).GetCell(4).SetText(checkdepar);              //测试部门
                    table.GetRow(tableindex).GetCell(5).SetText(urgentlevel);             //紧急程度
                    table.GetRow(tableindex).GetCell(6).SetText(remarks);                 //备注

                    tableindex++;
                }

                //写入文件
                using (MemoryStream ms = new MemoryStream())
                {
                    doc.Write(ms);
                    FileStream dumpFile = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
                    ms.WriteTo(dumpFile);
                    ms.Flush();
                    ms.Position = 0;
                    dumpFile.Close();
                }
            }
        }