|
@@ -1,7 +1,6 @@
|
|
|
package com.diagbot.facade;
|
|
|
|
|
|
import com.diagbot.entity.Ex;
|
|
|
-import com.diagbot.util.ListUtil;
|
|
|
import com.diagbot.util.ReflectUtil;
|
|
|
import com.diagbot.util.StringUtil;
|
|
|
import com.google.common.collect.Lists;
|
|
@@ -107,14 +106,10 @@ public class XLSXCovertCSVReader {
|
|
|
/**
|
|
|
* Accepts objects needed while parsing.
|
|
|
*
|
|
|
- * @param styles
|
|
|
- * Table of styles
|
|
|
- * @param strings
|
|
|
- * Table of shared strings
|
|
|
- * @param cols
|
|
|
- * Minimum number of columns to show
|
|
|
- * @param target
|
|
|
- * Sink for output
|
|
|
+ * @param styles Table of styles
|
|
|
+ * @param strings Table of shared strings
|
|
|
+ * @param cols Minimum number of columns to show
|
|
|
+ * @param target Sink for output
|
|
|
*/
|
|
|
public MyXSSFSheetHandler(StylesTable styles,
|
|
|
ReadOnlySharedStringsTable strings, int cols, PrintStream target) {
|
|
@@ -163,26 +158,27 @@ public class XLSXCovertCSVReader {
|
|
|
this.formatString = null;
|
|
|
String cellType = attributes.getValue("t");
|
|
|
String cellStyleStr = attributes.getValue("s");
|
|
|
- if ("b".equals(cellType))
|
|
|
+ if ("b".equals(cellType)) {
|
|
|
nextDataType = xssfDataType.BOOL;
|
|
|
- else if ("e".equals(cellType))
|
|
|
+ } else if ("e".equals(cellType)) {
|
|
|
nextDataType = xssfDataType.ERROR;
|
|
|
- else if ("inlineStr".equals(cellType))
|
|
|
+ } else if ("inlineStr".equals(cellType)) {
|
|
|
nextDataType = xssfDataType.INLINESTR;
|
|
|
- else if ("s".equals(cellType))
|
|
|
+ } else if ("s".equals(cellType)) {
|
|
|
nextDataType = xssfDataType.SSTINDEX;
|
|
|
- else if ("str".equals(cellType))
|
|
|
+ } else if ("str".equals(cellType)) {
|
|
|
nextDataType = xssfDataType.FORMULA;
|
|
|
- else if (cellStyleStr != null) {
|
|
|
+ } else if (cellStyleStr != null) {
|
|
|
// It's a number, but almost certainly one
|
|
|
// with a special style or format
|
|
|
int styleIndex = Integer.parseInt(cellStyleStr);
|
|
|
XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
|
|
|
this.formatIndex = style.getDataFormat();
|
|
|
this.formatString = style.getDataFormatString();
|
|
|
- if (this.formatString == null)
|
|
|
+ if (this.formatString == null) {
|
|
|
this.formatString = BuiltinFormats
|
|
|
.getBuiltinFormat(this.formatIndex);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -211,20 +207,20 @@ public class XLSXCovertCSVReader {
|
|
|
break;
|
|
|
|
|
|
case ERROR:
|
|
|
- thisStr = "\"ERROR:" + value.toString() + '"';
|
|
|
+ thisStr = "ERROR:" + value.toString();
|
|
|
break;
|
|
|
|
|
|
case FORMULA:
|
|
|
// A formula could result in a string value,
|
|
|
// so always add double-quote characters.
|
|
|
- thisStr = '"' + value.toString() + '"';
|
|
|
+ thisStr = value.toString();
|
|
|
break;
|
|
|
|
|
|
case INLINESTR:
|
|
|
// TODO: have seen an example of this, so it's untested.
|
|
|
XSSFRichTextString rtsi = new XSSFRichTextString(
|
|
|
value.toString());
|
|
|
- thisStr = '"' + rtsi.toString() + '"';
|
|
|
+ thisStr = rtsi.toString();
|
|
|
break;
|
|
|
|
|
|
case SSTINDEX:
|
|
@@ -233,7 +229,7 @@ public class XLSXCovertCSVReader {
|
|
|
int idx = Integer.parseInt(sstIndex);
|
|
|
XSSFRichTextString rtss = new XSSFRichTextString(
|
|
|
sharedStringsTable.getEntryAt(idx));
|
|
|
- thisStr = '"' + rtss.toString() + '"';
|
|
|
+ thisStr = rtss.toString();
|
|
|
} catch (NumberFormatException ex) {
|
|
|
output.println("Failed to parse SST index '" + sstIndex
|
|
|
+ "': " + ex.toString());
|
|
@@ -245,14 +241,15 @@ public class XLSXCovertCSVReader {
|
|
|
// 判断是否是日期格式
|
|
|
if (HSSFDateUtil.isADateFormat(this.formatIndex, n)) {
|
|
|
Double d = Double.parseDouble(n);
|
|
|
- Date date=HSSFDateUtil.getJavaDate(d);
|
|
|
- thisStr=formateDateToString(date);
|
|
|
- } else if (this.formatString != null)
|
|
|
+ Date date = HSSFDateUtil.getJavaDate(d);
|
|
|
+ thisStr = formateDateToString(date);
|
|
|
+ } else if (this.formatString != null) {
|
|
|
thisStr = formatter.formatRawCellContents(
|
|
|
Double.parseDouble(n), this.formatIndex,
|
|
|
this.formatString);
|
|
|
- else
|
|
|
+ } else {
|
|
|
thisStr = n;
|
|
|
+ }
|
|
|
break;
|
|
|
|
|
|
default:
|
|
@@ -271,11 +268,11 @@ public class XLSXCovertCSVReader {
|
|
|
}
|
|
|
record[thisColumn] = thisStr;
|
|
|
// Update column
|
|
|
- if (thisColumn > -1)
|
|
|
+ if (thisColumn > -1) {
|
|
|
lastColumnNumber = thisColumn;
|
|
|
+ }
|
|
|
|
|
|
} else if ("row".equals(name)) {
|
|
|
-
|
|
|
// Print out any missing commas if needed
|
|
|
if (minColumns > 0) {
|
|
|
// Columns are 0 based
|
|
@@ -311,8 +308,9 @@ public class XLSXCovertCSVReader {
|
|
|
*/
|
|
|
public void characters(char[] ch, int start, int length)
|
|
|
throws SAXException {
|
|
|
- if (vIsOpen)
|
|
|
+ if (vIsOpen) {
|
|
|
value.append(ch, start, length);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -348,12 +346,9 @@ public class XLSXCovertCSVReader {
|
|
|
/**
|
|
|
* Creates a new XLSX -> CSV converter
|
|
|
*
|
|
|
- * @param pkg
|
|
|
- * The XLSX package to process
|
|
|
- * @param output
|
|
|
- * The PrintStream to output the CSV to
|
|
|
- * @param minColumns
|
|
|
- * The minimum number of columns to output, or -1 for no minimum
|
|
|
+ * @param pkg The XLSX package to process
|
|
|
+ * @param output The PrintStream to output the CSV to
|
|
|
+ * @param minColumns The minimum number of columns to output, or -1 for no minimum
|
|
|
*/
|
|
|
public XLSXCovertCSVReader(OPCPackage pkg, PrintStream output,
|
|
|
String sheetName, int minColumns) {
|
|
@@ -414,25 +409,15 @@ public class XLSXCovertCSVReader {
|
|
|
++index;
|
|
|
}
|
|
|
}
|
|
|
- if (ListUtil.isNotEmpty(list)) {
|
|
|
- for (String[] arr : list) {
|
|
|
- for (int i = 0; i < arr.length; i++) {
|
|
|
- arr[i] = getObject(arr[i]);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 读取Excel
|
|
|
*
|
|
|
- * @param path
|
|
|
- * 文件路径
|
|
|
- * @param sheetName
|
|
|
- * sheet名称
|
|
|
- * @param minColumns
|
|
|
- * 列总数
|
|
|
+ * @param path 文件路径
|
|
|
+ * @param sheetName sheet名称
|
|
|
+ * @param minColumns 列总数
|
|
|
* @return
|
|
|
* @throws SAXException
|
|
|
* @throws ParserConfigurationException
|
|
@@ -463,7 +448,7 @@ public class XLSXCovertCSVReader {
|
|
|
* @throws SAXException
|
|
|
*/
|
|
|
private static List<String[]> readerExcel(InputStream inputStream, String sheetName,
|
|
|
- int minColumns) throws IOException, OpenXML4JException,
|
|
|
+ int minColumns) throws IOException, OpenXML4JException,
|
|
|
ParserConfigurationException, SAXException {
|
|
|
OPCPackage p = OPCPackage.open(inputStream);
|
|
|
XLSXCovertCSVReader xlsx2csv = new XLSXCovertCSVReader(p, System.out,
|
|
@@ -473,22 +458,6 @@ public class XLSXCovertCSVReader {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 去除前后的""
|
|
|
- *
|
|
|
- * @param s
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getObject(String s) {
|
|
|
- if (StringUtil.isBlank(s)) {
|
|
|
- return s;
|
|
|
- }
|
|
|
- if (s.startsWith("\"") && s.endsWith("\"")) {
|
|
|
- s = s.substring(1, s.length() -1);
|
|
|
- }
|
|
|
- return s;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 读取数据
|
|
|
*
|