NPOI2.1.1创建docx图表

      NPOI2.1.1版在2014年6月已发布,但对于word的操作还是不尽人意,如何利用NPOI在docx中插入图表方面网上几乎没有。经过对docx文件中插入图表的xml分析,对NPOI2.1.1源码进行了插入图表方面的修改,实现了inline和anchor两种方式的图表插入。

一、docx插入图表与inline和anchor关系

      与docx插图方式一样,用inline方式插入的图表位置不能灵活控制,只能通过段设置,对应word的嵌入型插入图表。用anchor方式插入的图表位置能灵活控制,对应word的四周型、紧密型、穿越型等。

二、docx文件中插入图表的xml分析

      docx插入图表的所有信息都包含在word文件夹中,文件夹中有document.xml文件、embeddings、charts和_rels文件夹。图表原始数据以xlsx文件格式存储在embeddings文件夹中,每一张图表对应一个xlsx文件。charts文件夹存储定义每张图表的chartn.xml文件及_rels文件夹,每个定义图表的chartn.xml文件通过_reles文件夹中对应的chartn.xml.rels文件建立图表与对应的xlsx文件关联。在_rels文件夹中的document.xml.rels文件对插入图表定义了以"rId"+数字组成的标识与图表关联。document.xml文件则存储着插入图表等的xml信息。

三、修改NPOI2.1.1源码

      NPOI中的OpenXmlFormats提供了较为完善的图表插入所有功能,但在XWPF中没有提供插入图表的方法。因此在XWPFRun类中增加了inline和anchor方式的AddChartSpace两个方法,实现AddChartSpace两个方法的代码由于较长,在此不列举。假如对图表操作较为熟悉,也可以不用提供的AddChartSpace方法实现docx插入图表。

四、inline插入饼图表实现代码

            XWPFDocument m_Docx = new XWPFDocument();
            //插入图表(饼图)
            //创建xlsx
            XSSFWorkbook workbook = new XSSFWorkbook();
            //创建表单1(饼图)
            ISheet sheet = workbook.CreateSheet("Sheet1");
            //表单1饼图数据
            //         销售额
            //第一季度 8.2
            //第二季度 3.2
            //第三季度 1.4
            //第四季度 1.2
            IRow row = sheet.CreateRow(0);
            ICell cell = row.CreateCell(0);
            cell = row.CreateCell(0);
            cell = row.CreateCell(1);
            cell.SetCellValue("销售额");
            row = sheet.CreateRow(1);
            cell = row.CreateCell(0);
            cell.SetCellValue("第一季度");
            cell = row.CreateCell(1);
            cell.SetCellValue(8.2);
            row = sheet.CreateRow(2);
            cell = row.CreateCell(0);
            cell.SetCellValue("第二季度");
            cell = row.CreateCell(1);
            cell.SetCellValue(3.2);
            row = sheet.CreateRow(3);
            cell = row.CreateCell(0);
            cell.SetCellValue("第三季度");
            cell = row.CreateCell(1);
            cell.SetCellValue(1.4);
            row = sheet.CreateRow(4);
            cell = row.CreateCell(0);
            cell.SetCellValue("第四季度");
            cell = row.CreateCell(1);
            cell.SetCellValue(1.2);
            //创建\word\charts\chartn.xml内容(简单饼图)
            CT_ChartSpace ctpiechartspace = new CT_ChartSpace();

            ctpiechartspace.date1904 = new CT_Boolean();
            ctpiechartspace.date1904.val = 1;
            ctpiechartspace.lang = new CT_TextLanguageID();
            ctpiechartspace.lang.val = "zh-CN";

            CT_Chart m_chart = ctpiechartspace.AddNewChart();
            m_chart.plotArea = new CT_PlotArea();
            m_chart.plotArea.pieChart = new List<CT_PieChart>();
            //饼图
            CT_PieChart m_piechart = new CT_PieChart();
            m_piechart.varyColors = new CT_Boolean();
            m_piechart.varyColors.val = 1;
            m_piechart.ser = new List<CT_PieSer>();
            CT_PieSer m_pieser = new CT_PieSer();
            //标题
            m_pieser.tx = new CT_SerTx();
            m_pieser.tx.strRef = new CT_StrRef();
            m_pieser.tx.strRef.f = "Sheet1!$B$1";
            m_pieser.tx.strRef.strCache = new CT_StrData();
            m_pieser.tx.strRef.strCache.ptCount = new CT_UnsignedInt();
            m_pieser.tx.strRef.strCache.ptCount.val = 1;
            CT_StrVal m_strval = new CT_StrVal();
            m_strval.idx = 0;
            m_strval.v = "销售额";
            m_pieser.tx.strRef.strCache.pt = new List<CT_StrVal>();
            m_pieser.tx.strRef.strCache.pt.Add(m_strval);
            //行标题
            m_pieser.cat = new CT_AxDataSource();
            m_pieser.cat.strRef = new CT_StrRef();
            m_pieser.cat.strRef.f = "Sheet1!$A$2:$A$5";
            m_pieser.cat.strRef.strCache = new CT_StrData();
            m_pieser.cat.strRef.strCache.ptCount = new CT_UnsignedInt();
            m_pieser.cat.strRef.strCache.ptCount.val = 4;
            m_pieser.cat.strRef.strCache.pt = new List<CT_StrVal>();
            m_strval = new CT_StrVal();
            m_strval.idx = 0;
            m_strval.v = "第一季度";
            m_pieser.cat.strRef.strCache.pt.Add(m_strval);
            m_strval = new CT_StrVal();
            m_strval.idx = 1;
            m_strval.v = "第二季度";
            m_pieser.cat.strRef.strCache.pt.Add(m_strval);
            m_strval = new CT_StrVal();
            m_strval.idx = 2;
            m_strval.v = "第三季度";
            m_pieser.cat.strRef.strCache.pt.Add(m_strval);
            m_strval = new CT_StrVal();
            m_strval.idx = 3;
            m_strval.v = "第四季度";
            m_pieser.cat.strRef.strCache.pt.Add(m_strval);
            //值
            m_pieser.val = new CT_NumDataSource();
            m_pieser.val.numRef = new CT_NumRef();
            m_pieser.val.numRef.f = "Sheet1!$B$2:$B$5";
            m_pieser.val.numRef.numCache = new CT_NumData();
            m_pieser.val.numRef.numCache.formatCode = "General";
            m_pieser.val.numRef.numCache.ptCount = new CT_UnsignedInt();
            m_pieser.val.numRef.numCache.ptCount.val = 4;
            m_pieser.val.numRef.numCache.pt = new List<CT_NumVal>();
            CT_NumVal m_numval = new CT_NumVal();
            m_numval.idx = 0;
            m_numval.v = "8.2";
            m_pieser.val.numRef.numCache.pt.Add(m_numval);
            m_numval = new CT_NumVal();
            m_numval.idx = 1;
            m_numval.v = "3.2";
            m_pieser.val.numRef.numCache.pt.Add(m_numval);
            m_numval = new CT_NumVal();
            m_numval.idx = 2;
            m_numval.v = "1.4";
            m_pieser.val.numRef.numCache.pt.Add(m_numval);
            m_numval = new CT_NumVal();
            m_numval.idx = 3;
            m_numval.v = "1.2";
            m_pieser.val.numRef.numCache.pt.Add(m_numval);
            m_piechart.ser.Add(m_pieser);

            m_chart.plotArea.pieChart.Add(m_piechart);

            m_chart.legend = new CT_Legend();
            m_chart.legend.legendPos = new CT_LegendPos();
            m_chart.legend.legendPos.val = ST_LegendPos.r;
            m_chart.plotVisOnly = new CT_Boolean();
            m_chart.plotVisOnly.val = 1;

            XWPFParagraph gp = m_Docx.CreateParagraph();
            XWPFRun gr = gp.CreateRun();
            gp = m_Docx.CreateParagraph();
            gr = gp.CreateRun();
            gr.AddChartSpace(workbook , ctpiechartspace, 5274310, 3076575);

       经过调试的例子下载:http://download.csdn.net/detail/gltide/7963395。NPOI是tonyqus提供的2.1.1.0源码经过修改编译,创建的docx在word2007可以打开,例子创建anchor方式的饼图效果图如下。




©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页