Skip to content

Commit

Permalink
删除冗余代码
Browse files Browse the repository at this point in the history
  • Loading branch information
liaochong committed Jan 26, 2022
1 parent 094281f commit b90af19
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
Expand Down Expand Up @@ -356,7 +355,7 @@ private void process(OPCPackage xlsxPackage) throws IOException, OpenXML4JExcept
this.doReadSheet(xlsxPackage, (stream, index, sheetName) -> {
readConfig.startSheetConsumer.accept(sheetName, index);
ContentHandler handler = new XSSFSheetXMLHandler(
mergeCellIndexMapping.getOrDefault(index, Collections.emptyMap()), strings, new XSSFSaxReadHandler<>(result, readConfig), new DataFormatter());
mergeCellIndexMapping.getOrDefault(index, Collections.emptyMap()), strings, new XSSFSaxReadHandler<>(result, readConfig));
processSheet(handler, stream);
mergeCellIndexMapping.remove(index);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.constant.Constants;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.model.SharedStrings;
Expand Down Expand Up @@ -72,10 +71,6 @@ enum xssfDataType {
// used when cell close element is seen.
private xssfDataType nextDataType;

// Used to format numeric cell values.
private short formatIndex;
private String formatString;
private final DataFormatter formatter;
private int rowNum;
private int preRowNum = -1;
private int nextRowNum; // some sheets do not have rowNums, Excel can read them so we should try to handle them correctly as well
Expand All @@ -93,19 +88,16 @@ enum xssfDataType {
*
* @param strings Table of shared strings
* @param sheetContentsHandler sheetContentsHandler
* @param dataFormatter dataFormatter
*/
public XSSFSheetXMLHandler(
Map<CellAddress, CellAddress> mergeCellMapping,
SharedStrings strings,
XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler,
DataFormatter dataFormatter) {
XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler) {
this.mergeCellMapping = mergeCellMapping;
this.mergeFirstCellMapping = mergeCellMapping.values().stream().distinct().collect(Collectors.toMap(cellAddress -> cellAddress, c -> ""));
this.sharedStringsTable = strings;
this.output = sheetContentsHandler;
this.nextDataType = xssfDataType.NUMBER;
this.formatter = dataFormatter;
}

private boolean isTextTag(String name) {
Expand Down Expand Up @@ -140,8 +132,6 @@ public void startElement(String uri, String localName, String qName,
else if ("c".equals(localName)) {
// Set up defaults.
this.nextDataType = xssfDataType.NUMBER;
this.formatIndex = -1;
this.formatString = null;
cellRef = attributes.getValue("r");
String cellType = attributes.getValue("t");
String cellStyleStr = attributes.getValue("s");
Expand Down Expand Up @@ -178,14 +168,6 @@ else if ("str".equals(cellType))
if (nextDataType == xssfDataType.NUMBER) {
nextDataType = xssfDataType.FORMULA;
}

// Decide where to get the formula string from
String type = attributes.getValue("t");
if (type != null && type.equals("shared")) {
// Is it the one that defines the shared, or uses it?
String ref = attributes.getValue("ref");
String si = attributes.getValue("si");
}
}
}

Expand Down Expand Up @@ -215,21 +197,8 @@ public void endElement(String uri, String localName, String qName)
break;

case FORMULA:
String fv = value.toString();

if (this.formatString != null) {
try {
// Try to use the value as a formattable number
double d = Double.parseDouble(fv);
thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString);
} catch (NumberFormatException e) {
// Formula is a String result not a Numeric one
thisStr = fv;
}
} else {
// No formatting applied, just do raw value in all cases
thisStr = fv;
}
// No formatting applied, just do raw value in all cases
thisStr = value.toString();
break;
case INLINE_STRING:
// TODO: Can these ever have formatting on them?
Expand All @@ -250,14 +219,10 @@ public void endElement(String uri, String localName, String qName)

case NUMBER:
String n = value.toString();
if (this.formatString != null && n.length() > 0)
thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
else {
if (n.contains(Constants.SPOT)) {
n = String.valueOf(Double.parseDouble(n));
}
thisStr = n;
if (n.contains(Constants.SPOT)) {
n = String.valueOf(Double.parseDouble(n));
}
thisStr = n;
break;

default:
Expand Down

0 comments on commit b90af19

Please sign in to comment.