|
|
|
@ -4,12 +4,15 @@ import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import com.dsideal.base.DataEase.Model.ExcelReader;
|
|
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
|
import org.apache.poi.openxml4j.util.ZipSecureFile;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFFont;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFChart;
|
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
|
|
import org.dom4j.Document;
|
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
|
import org.dom4j.Element;
|
|
|
|
@ -353,10 +356,11 @@ public class ExcelKit {
|
|
|
|
|
* @return 结果数据
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) throws IOException, DocumentException {
|
|
|
|
|
List<List<String>> data = readChart(docPath, chartNumber);
|
|
|
|
|
public static List<List<String>> getChartData(String docPath, int chartNumber, int skipRowCount) throws IOException, DocumentException, InterruptedException, InvalidFormatException {
|
|
|
|
|
ExcelKit.UnCompress(docPath);
|
|
|
|
|
List<List<String>> data = readChartByXml(docPath, chartNumber);
|
|
|
|
|
//需要跳过前skipRowCount行,先要判断一下是不是够跳的,不够跳的直接输出错误
|
|
|
|
|
if (data == null || data.size() < skipRowCount) {
|
|
|
|
|
if (data.size() < skipRowCount) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
//截取部分数据返回
|
|
|
|
@ -520,7 +524,7 @@ public class ExcelKit {
|
|
|
|
|
*
|
|
|
|
|
* @param chartNumber
|
|
|
|
|
*/
|
|
|
|
|
public static List<List<String>> readChart(String sourceDoc, int chartNumber) throws DocumentException, IOException {
|
|
|
|
|
public static List<List<String>> readChartByXml(String sourceDoc, int chartNumber) throws DocumentException, IOException {
|
|
|
|
|
//将word文件解压缩
|
|
|
|
|
UnCompress(sourceDoc);
|
|
|
|
|
List<List<String>> matrix = new ArrayList<>();
|
|
|
|
@ -529,7 +533,7 @@ public class ExcelKit {
|
|
|
|
|
System.out.println(sourceDoc + " 没有找到第" + chartNumber + "个图表");
|
|
|
|
|
return matrix;
|
|
|
|
|
}
|
|
|
|
|
return readChart(xml);
|
|
|
|
|
return readChartByXml(xml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -560,7 +564,7 @@ public class ExcelKit {
|
|
|
|
|
* @return
|
|
|
|
|
* @throws DocumentException
|
|
|
|
|
*/
|
|
|
|
|
public static List<List<String>> readChart(String xmlPath) throws DocumentException {
|
|
|
|
|
public static List<List<String>> readChartByXml(String xmlPath) throws DocumentException {
|
|
|
|
|
//图表类型
|
|
|
|
|
String[] CHART_TYPES = {"lineChart", "barChart"};//折线,柱状
|
|
|
|
|
|
|
|
|
@ -574,9 +578,14 @@ public class ExcelKit {
|
|
|
|
|
List<Element> children = root.element("chart").element("plotArea").elements();//工作区
|
|
|
|
|
boolean hadReadXList = false;//是不是已经读取过横坐标也就是年份的数据了
|
|
|
|
|
for (Element child : children) {
|
|
|
|
|
//System.out.println("当前遍历到的元素是:" + child.getName());
|
|
|
|
|
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);//将CHART_TYPES数组转换为List
|
|
|
|
|
if (child.getName().endsWith("Chart") && !CHART_TYPES_LIST.contains(child.getName())) {
|
|
|
|
|
System.out.println("发现除线形图和柱状图以外的图表类型,本系统不支持这种类型,请人为修改:" + child.getName());
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CHART_TYPES_LIST.contains(child.getName())) {//如果当前遍历到的子元素是折线图或者柱状图
|
|
|
|
|
//System.out.println("找到图表类型:" + child.getName());
|
|
|
|
|
String type = child.getName();
|
|
|
|
|
//ce:chart element
|
|
|
|
|
Element ce = root.element("chart").element("plotArea").element(type);
|
|
|
|
@ -664,4 +673,32 @@ public class ExcelKit {
|
|
|
|
|
if (s.contains("百")) return 100;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过POI读取数据
|
|
|
|
|
*
|
|
|
|
|
* @param docPath
|
|
|
|
|
* @param chartNumber
|
|
|
|
|
* @param skipRowCount
|
|
|
|
|
* @return
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws DocumentException
|
|
|
|
|
* @throws InterruptedException
|
|
|
|
|
* @throws InvalidFormatException
|
|
|
|
|
*/
|
|
|
|
|
public static List<List<String>> readChartByPOI(String docPath, int chartNumber, int skipRowCount) throws IOException, DocumentException, InterruptedException, InvalidFormatException {
|
|
|
|
|
InputStream is = new FileInputStream(docPath);
|
|
|
|
|
ZipSecureFile.setMinInflateRatio(-1.0d);
|
|
|
|
|
XWPFDocument doc = new XWPFDocument(is);
|
|
|
|
|
//排序后的图表
|
|
|
|
|
List<XWPFChart> charts = ExcelKit.getSortListForXWPFChart(doc.getCharts());
|
|
|
|
|
if (charts.size() < chartNumber) {
|
|
|
|
|
System.out.println(docPath + ",没有第" + chartNumber + "个图表,请检查!");
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
}
|
|
|
|
|
XSSFWorkbook workbook = charts.get(chartNumber - 1).getWorkbook();
|
|
|
|
|
List<List<String>> data = ExcelKit.readSheet(workbook, skipRowCount);
|
|
|
|
|
is.close();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|