2017-05-08 3 views
0

Iv는 XWPF 문서를 몇 주 동안 사용해 왔으며 차트를 추가 할 수 없었습니다. 원형 차트, 막 대형 차트. 수동으로 XML 차트를 파일에 삽입 할 계획이지만 과도하다고 생각합니다. Docx 템플릿에 차트를 추가하기 만하면됩니다. Aspose와 javadocx는 옵션이 아닙니다.Apache POI Java - Docx 차트 및 그래프

XWPFDocument document = new XWPFDocument(getClass().getResourceAsStream("/templates/standard.docx")); 
//INSERT PIE CHART 
FileOutputStream out = new FileOutputStream(new File("output/output.docx"); 
document.write(out); 

[UPDATE - 쉬운 경로]

때문에 시간이 성공적으로 주입 방법을 쓰는 데 걸리는은, IV는 편리한 (빠른 N 더러운) 차트를 추가하는 방법을 발견했다. 이것은 일반적인 단어 차트가 아니라 라이브러리에서 생성되고 그림으로 저장되어 삽입됩니다.

먼저 라이브러리를 http://knowm.org/open-source/xchart/xchart-example-code에서 다운로드했습니다.

둘째, XWPFdocument를 구현 한 사람은 차트를 만들어 이미지로 추가합니다.

private XWPFDocument add_chart(XWPFDocument document) 
{ 
    // New Chart Element 
    CategoryChart chart = new CategoryChartBuilder().width(500).height(400).theme(Styler.ChartTheme.GGPlot2).title(getClass().getSimpleName()).build(); 
    chart.setTitle("Issue Count"); 
    // Customize Chart 
    Color[] sliceColors = new Color[]{new Color(27, 50, 119), new Color(58, 146, 56), new Color(0, 161, 222), new Color(154, 205, 102), new Color(246, 199, 182)}; 
    chart.getStyler().setSeriesColors(sliceColors); 

    // Series 
    chart.addSeries("Critical", new ArrayList<>(Arrays.asList(new String[]{"Count"})), new ArrayList<>(Arrays.asList(new Number[]{10}))); 
    chart.addSeries("High", new ArrayList<>(Arrays.asList(new String[]{"High"})), new ArrayList<>(Arrays.asList(new Number[]{5}))); 
    chart.addSeries("Medium", new ArrayList<>(Arrays.asList(new String[]{"Medium"})), new ArrayList<>(Arrays.asList(new Number[]{2}))); 
    chart.addSeries("Low", new ArrayList<>(Arrays.asList(new String[]{"Low"})), new ArrayList<>(Arrays.asList(new Number[]{1}))); 

    // Create and store a jpg image of the chart, then append it to the document 
    BitmapEncoder.saveBitmapWithDPI(chart, "tmp.jpg", BitmapFormat.JPG, 300); 
    document.createParagraph().createRun().addPicture(new FileInputStream("tmp.jpg"), XWPFDocument.PICTURE_TYPE_JPEG, "tmp.jpg", Units.toEMU(500), Units.toEMU(400)); 
    return document; 
} 

내가 라이브러리를 사용하여 만든 하나 개의 차트의 예 : POI-chart-example

+0

삽입 코드가 여기^ –

+2

네, 당신은 꽤 많이 자신의 것을 작성해야 할거야 MS-WORD 파일에서 차트를 읽을 수있는 간단한 예입니다. 차트는 XSSF에서만 최소로 지원되며 XWPF에서는 지원되지 않습니다. CT 수업을 사용하여 직접 작성해야합니다. 차트 유형 2 ~ 3 가지를 해결하면 커뮤니티에 기부금을 환급받을 수 있습니다. – jmarkmurphy

답변

0

읽고 문서 파일의 차트를 수정 한 다음 실제 문서 파일로 작성하는 위치의 사용자 정의 포이 항아리 아래에 사용할 수 있습니다. XWPFChart 클래스를 사용하면 POI EXCEL/PPT에서 사용할 수있는 모든 메서드를 사용할 수 있습니다.

https://github.com/sandeeptiwari32/POI_ENHN/POI3.14.jar

아래

public class PoiDocTest { 
    public static void main(String arg[]) throws FileNotFoundException, IOException 
    { 
     @SuppressWarnings("resource") 
     XWPFDocument document = new XWPFDocument(new FileInputStream("chart.docx")); 
     @SuppressWarnings("unused") 
     XWPFChart chart; 
     for (POIXMLDocumentPart part : document.getRelations()) { 
      if (part instanceof XWPFChart) { 
       chart = (XWPFChart) part; 
       break; 
      } 
     } 
    } 
}