一、POM
< dependency> < groupId> org. apache. poi< / groupId> < artifactId> poi< / artifactId> < version> 4.1 .0 < / version> < / dependency> < dependency> < groupId> org. apache. poi< / groupId> < artifactId> poi- ooxml< / artifactId> < version> 4.1 .0 < / version> < / dependency> < dependency> < groupId> org. apache. poi< / groupId> < artifactId> poi- ooxml- schemas< / artifactId> < version> 4.1 .0 < / version> < / dependency>
二、Word模板
三、工具类
import com. google. common. base. Strings;
import org. apache. poi. ss. util. CellRangeAddress;
import org. apache. poi. ss. util. CellReference;
import org. apache. poi. xddf. usermodel. chart. XDDFChartData;
import org. apache. poi. xddf. usermodel. chart. XDDFDataSource;
import org. apache. poi. xddf. usermodel. chart. XDDFDataSourcesFactory;
import org. apache. poi. xddf. usermodel. chart. XDDFNumericalDataSource;
import org. apache. poi. xssf. usermodel. XSSFSheet;
import org. apache. poi. xwpf. usermodel. XWPFChart;
public class PoiUtil { public static XWPFChart wordExportChar ( XWPFChart docChart, String title, String[ ] seriesNames, XSSFSheet sheet ) { XDDFChartData chartData = docChart. getChartSeries ( ) . get ( 0 ) ; XDDFDataSource catDataSource = XDDFDataSourcesFactory. fromStringCellRange ( sheet, new CellRangeAddress ( 1 , 4 , 0 , 0 ) ) ; for ( int i = 0 ; i < seriesNames. length; i++ ) { XDDFNumericalDataSource< Double> valDataSource = XDDFDataSourcesFactory. fromNumericCellRange ( sheet, new CellRangeAddress ( 1 , 4 , 1 , 1 ) ) ; XDDFChartData. Series series = chartData. getSeries ( ) . get ( i) ; series. replaceData ( catDataSource, valDataSource) ; CellReference cellReference = docChart. setSheetTitle ( seriesNames[ i] , 1 ) ; series. setTitle ( seriesNames[ i] , cellReference) ; } docChart. plot ( chartData) ; if ( ! Strings. isNullOrEmpty ( title) ) { docChart. setTitleText ( title) ; docChart. setTitleOverlay ( false ) ; } return docChart; }
}
四、测试类
public class TestWord3 { public static void main ( String[ ] args) throws Exception { InputStream is = Thread. currentThread ( ) . getContextClassLoader ( ) . getResourceAsStream ( "111.docx" ) ; assert is != null ; XWPFDocument doc = new XWPFDocument ( is) ; List< XWPFChart> charts = doc. getCharts ( ) ; XWPFChart chart = charts. get ( 0 ) ; XSSFWorkbook workbook = chart. getWorkbook ( ) ; XSSFSheet sheet = workbook. getSheetAt ( 0 ) ; XWPFChart singleBarChar = charts. get ( 0 ) ;
String[ ] singleBarSeriesNames = { "运动情况" } ;
refreshExcel ( sheet) ; PoiUtil. wordExportChar ( singleBarChar, "运动情况" , singleBarSeriesNames, sheet ) ; try ( FileOutputStream fos = new FileOutputStream ( "D:\\test8.docx" ) ) { doc. write ( fos) ; } is. close ( ) ; doc. close ( ) ; } public static void refreshExcel ( XSSFSheet sheet ) { sheet. getRow ( 1 ) . getCell ( 0 ) . setCellValue ( "走路" ) ; sheet. getRow ( 2 ) . getCell ( 0 ) . setCellValue ( "跑步" ) ; sheet. getRow ( 3 ) . getCell ( 0 ) . setCellValue ( "游泳" ) ; sheet. getRow ( 4 ) . getCell ( 0 ) . setCellValue ( "跳高" ) ; sheet. getRow ( 1 ) . getCell ( 1 ) . setCellValue ( 100 ) ; sheet. getRow ( 2 ) . getCell ( 1 ) . setCellValue ( 150 ) ; sheet. getRow ( 3 ) . getCell ( 1 ) . setCellValue ( 50 ) ; sheet. getRow ( 4 ) . getCell ( 1 ) . setCellValue ( 200 ) ; }
}
五、运行结果