为什么引用数据透视表的工式显示错误?
下面是代码:
package com.wczw;
import com.grapecity.documents.excel.DeserializationOptions;
import com.grapecity.documents.excel.IWorksheet;
import com.grapecity.documents.excel.PdfSaveOptions;
import com.grapecity.documents.excel.Workbook;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JsonToExcelTest03 {
private static final Log log = LogFactory.getLog(JsonToExcelTest03.class);
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public static void main(String[] args) throws IOException {
// 测试目录
File folder = new File("test");
// 输入文件
File excelIn = new File(folder, "简单Demo透视表和VLOOKUP.xlsx");
// 输出文件PDF
File pdf1 = new File(folder, "简单Demo透视表和VLOOKUP-out1.pdf");
File pdf2 = new File(folder, "简单Demo透视表和VLOOKUP-out2.pdf");
// // 输出文件Excel
File excelOut = new File(folder, "简单Demo透视表和VLOOKUP-out.xlsx");
Date startTime = new Date();
// 初始化Excel对象
Workbook workbook = new Workbook();
DeserializationOptions deserializationOptions = new DeserializationOptions();
// workbook.fromJson(json, deserializationOptions);
workbook.open(excelIn.getAbsolutePath());
// 修改数据
IWorksheet worksheet = workbook.getWorksheets().get(0);
int startLine = 2;
int endLine = 45;
Object[] o = new Object[endLine-startLine+1];
for (int i=0; i< o.length; i++) {
o = 558;
}
worksheet.getRange(String.format("D%d%d", startLine, endLine)).setValue(o);
worksheet = workbook.getWorksheets().get(1);
worksheet.getPivotTables().get(0).refresh();
// 导出为Excel
workbook.save(excelOut.getAbsolutePath());
// 导出为PDF
{
PdfSaveOptions options = new PdfSaveOptions();
worksheet = workbook.getWorksheets().get(1);
worksheet.save(pdf1.getAbsolutePath());
worksheet = workbook.getWorksheets().get(2);
worksheet.save(pdf2.getAbsolutePath());
}
Date endTime = new Date();
log.info(String.format("运行时间:%s -> %s,耗时:%d 秒", sdf.format(startTime), sdf.format(endTime), (endTime.getTime() - startTime.getTime())/1000 ));
}
}
下面是POM:<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>gcexceltest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.grapecity.documents</groupId>
<artifactId>gcexcel</artifactId>
<version>4.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.9</version>
</dependency>
</dependencies>
</project>
附件是测试用Excel
多谢!
|
|