public static XWPFDocument CreatePie(int numbers,String countent, IList<Double> datas, IList<String> legend, XWPFDocument doc, ST_LegendPos legendLocal, int x1 = 200000, int y1 = 0,int x2 = 6274310, int y2 = 3116575, String title = "销售额")
{
//数据接收
//左上坐标 x1, y1, 右上 x2,y2
//写入路径
//title
//图例
//图例位置
//内容文字
//anchor
XWPFDocument m_Docx = doc;
//页面设置
//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.w = (ulong)16838;
m_SectPr.pgSz.h = (ulong)11906;
//插入饼图图表
//插入xlsx
//创建xlsx
XSSFWorkbook workbook = new XSSFWorkbook();
//创建表单1(饼图)
ISheet sheet = workbook.CreateSheet("Sheet1");
//表单1饼图数据
// 销售额
//第一季度 8.2
//第二季度 3.2
//第三季度 1.4
//第四季度 1.2
//设置title
IRow row = sheet.CreateRow(0);
ICell cell = row.CreateCell(0);
cell = row.CreateCell(0);
cell = row.CreateCell(1);
cell.SetCellValue(title);
//饼图图例加数值
//判断 图例和数据是否一一对应
if(datas.Count != legend.Count){
throw new Exception("图例和数据数量不一样");
}
for (int i = 0; i < datas.Count; i++)
{
row = sheet.CreateRow(i+1);
cell = row.CreateCell(0);
cell.SetCellValue(legend[i]);
cell = row.CreateCell(1);
cell.SetCellValue(datas[i]);
}
//把xlsx存入内存流并转为字节流
MemoryStream msworkbook = new MemoryStream();
workbook.Write(msworkbook);
msworkbook.Flush();
byte[] data = msworkbook.ToArray();
msworkbook.Close();
//饼图1
//创建\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.title = new CT_Title();//标题采用tx的值
m_chart.title.overlay = new CT_Boolean();
m_chart.title.overlay.val = 0;
m_chart.autoTitleDeleted = new CT_Boolean();
m_chart.autoTitleDeleted.val = 0;
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_piechart.ser.Add(m_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 = title;
m_pieser.tx.strRef.strCache.pt = new List<CT_StrVal>();
m_pieser.tx.strRef.strCache.pt.Add(m_strval);
m_pieser.dLbls = new CT_DLbls();
m_pieser.dLbls.showLegendKey = new CT_Boolean() ;
m_pieser.dLbls.showLegendKey.val = 1;
m_pieser.dLbls.showVal = new CT_Boolean();
m_pieser.dLbls.showVal.val = 1;
m_pieser.dLbls.showCatName = new CT_Boolean();
m_pieser.dLbls.showCatName.val = 1;
//m_pieser.dLbls.showSerName = new CT_Boolean();
//m_pieser.dLbls.showSerName.val = 1;
m_pieser.dLbls.showPercent = new CT_Boolean();
m_pieser.dLbls.showPercent.val = 1;
m_pieser.dLbls.showBubbleSize = new CT_Boolean();
m_pieser.dLbls.showBubbleSize.val = 1;
m_pieser.dLbls.showLeaderLines = new CT_Boolean();
m_pieser.dLbls.showLeaderLines.val = 1;
//行标题
m_pieser.cat = new CT_AxDataSource();
m_pieser.cat.strRef = new CT_StrRef();
m_pieser.cat.strRef.f = "Sheet1!$A$2:$A$"+(datas.Count+1);
m_pieser.cat.strRef.strCache = new CT_StrData();
m_pieser.cat.strRef.strCache.ptCount = new CT_UnsignedInt();
m_pieser.cat.strRef.strCache.ptCount.val = (uint)numbers;
m_pieser.cat.strRef.strCache.pt = new List<CT_StrVal>();
//图例
for (uint i = 0; i < legend.Count; i++)
{
m_strval = new CT_StrVal();
m_strval.idx = i;
m_strval.v = legend[(int)i];
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$"+(datas.Count+1);
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 = (uint)numbers;
m_pieser.val.numRef.numCache.pt = new List<CT_NumVal>();
for (uint i = 0; i < datas.Count; i++)
{
CT_NumVal m_numval = new CT_NumVal();
m_numval.idx = i;
m_numval.v = datas[(int)i]+"";
m_pieser.val.numRef.numCache.pt.Add(m_numval);
}
m_piechart.ser.Add(m_pieser);
//图表与数据显示设计
m_piechart.dLbls = new CT_DLbls();
m_piechart.dLbls.showLegendKey = new CT_Boolean();
m_piechart.dLbls.showLegendKey.val = 0;
m_piechart.dLbls.showVal = new CT_Boolean();//值显示设置
m_piechart.dLbls.showVal.val = 0;
m_piechart.dLbls.showCatName = new CT_Boolean();//分类显示设置
m_piechart.dLbls.showCatName.val = 0;
m_piechart.dLbls.showSerName = new CT_Boolean();//tx标题显示设置
m_piechart.dLbls.showSerName.val = 0;
m_piechart.dLbls.showPercent = new CT_Boolean();
m_piechart.dLbls.showPercent.val = 1;
m_piechart.dLbls.showBubbleSize = new CT_Boolean();
m_piechart.dLbls.showBubbleSize.val = 0;
m_piechart.dLbls.showLeaderLines = new CT_Boolean();
m_piechart.dLbls.showLeaderLines.val = 1;
m_piechart.firstSliceAng = new CT_FirstSliceAng();
m_piechart.firstSliceAng.val = 0;
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 = legendLocal;
m_chart.plotVisOnly = new CT_Boolean();
m_chart.plotVisOnly.val = 1;
//四周-bisides
CT_WrapSquare wrapSquare = new CT_WrapSquare();
wrapSquare.wrapText = ST_WrapText.bothSides;
XWPFParagraph gp = m_Docx.CreateParagraph();
XWPFRun gr = gp.CreateRun();
gp = m_Docx.CreateParagraph();
gr = gp.CreateRun();
//左上坐标 x1, y1, 右上 x2,y2
gr.AddChartSpace(new XSSFWorkbook(new MemoryStream(data)), ctpiechartspace, wrapSquare, x1, y1, x2, y2);
XWPFParagraph Textgp = m_Docx.CreateParagraph();
XWPFRun Textgr = gp.CreateRun();
XWPFStyles m_styles = m_Docx.CreateStyles();
CT_RPr rpr3 = Textgr.GetCTR().AddNewRPr();
CT_Fonts rfonts3 = rpr3.AddNewRFonts();
rfonts3.ascii = "仿宋_GB2312";
rfonts3.eastAsia = "仿宋_GB2312";
rpr3.AddNewSz().val = (ulong)30;//1号字体
rpr3.AddNewSzCs().val = (ulong)30;
gr.SetText(countent);
return m_Docx;
}
使用npoi 导出可编辑的word饼图,仅限于office 2007
猜你喜欢
转载自blog.csdn.net/qq_26837711/article/details/80419531
今日推荐
周排行