问题是多维的。
首先,数字格式60############
不能用于应用Java
。它导致java.lang.IllegalArgumentException: Malformed pattern "60############"
使用DecimalFormat。
但是,如果需要的具有“60”为前缀的每个号码,则Excel
数字格式\6\0#
或"60"#
应该是可能的,并且应当被翻译成的DecimalFormat
图案'60'#
。但apache poi
的DataFormatter
没有,因为它只是从Excel
的格式字符串中删除所有引用,这导致60#
也是格式不正确。
问题出在DataFormatter.java:671ff。
我已经在我的MyDataFormatter
修补了这个像这样:
...
// Now, handle the other aspects like
// quoting and scientific notation
for(int i = 0; i < sb.length(); i++) {
char c = sb.charAt(i);
/*
// remove quotes and back slashes
if (c == '\\' || c == '"') {
sb.deleteCharAt(i);
i--;
*/
// handle quotes and back slashes
if (c == '\\') {
sb.setCharAt(i, '\'');
sb.insert(i+2, '\'');
i+=2;
} else if (c == '"') {
sb.setCharAt(i, '\'');
// for scientific/engineering notation
} else if (c == '+' && i > 0 && sb.charAt(i - 1) == 'E') {
sb.deleteCharAt(i);
i--;
}
}
formatStr = sb.toString();
formatStr = formatStr.replace("''", "");
return formatStr;
}
...
在这个例子中使用这样的:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.FileInputStream;
import java.lang.reflect.Method;
class ExcelDataformatterExample {
public static void main(String[] args) throws Exception {
Workbook wb = WorkbookFactory.create(new FileInputStream("ExcelExample.xlsx"));
DataFormatter df = new DataFormatter();
MyDataFormatter mydf = new MyDataFormatter();
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellTypeEnum() == CellType.NUMERIC) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
System.out.println("Cell " + cellRef.formatAsString());
System.out.print("Excel's data format string: ");
String formatStr = cell.getCellStyle().getDataFormatString();
System.out.println(formatStr);
System.out.print("Value using poi's data formatter: ");
Method cleanFormatForNumber = DataFormatter.class.getDeclaredMethod("cleanFormatForNumber", String.class);
cleanFormatForNumber.setAccessible(true);
String cleanFormatStr = (String)cleanFormatForNumber.invoke(df, formatStr);
System.out.print("using poi's cleanFormatStr: ");
System.out.print(cleanFormatStr + " result: ");
String value = df.formatCellValue(cell);
System.out.println(value);
System.out.print("Value using my data formatter: ");
cleanFormatForNumber = MyDataFormatter.class.getDeclaredMethod("cleanFormatForNumber", String.class);
cleanFormatForNumber.setAccessible(true);
cleanFormatStr = (String)cleanFormatForNumber.invoke(mydf, formatStr);
System.out.print("using my cleanFormatStr: ");
System.out.print(cleanFormatStr + " result: ");
value = mydf.formatCellValue(cell);
System.out.println(value);
}
}
}
wb.close();
}
}
它导致下面的输出,如果值是在细胞A1
至A4
格式化为Excel
如图所示:
Cell A1
Excel's data format string: \60##########
Value using poi's data formatter: using poi's cleanFormatStr: 60########## result: 166609647
Value using my data formatter: using my cleanFormatStr: '6'0########## result: 166609647
Cell A2
Excel's data format string: \60000000000
Value using poi's data formatter: using poi's cleanFormatStr: 60000000000 result: 60166609647
Value using my data formatter: using my cleanFormatStr: '6'0000000000 result: 60166609647
Cell A3
Excel's data format string: "60"#
Value using poi's data formatter: using poi's cleanFormatStr: 60# result: 166609647
Value using my data formatter: using my cleanFormatStr: '60'# result: 60166609647
Cell A4
Excel's data format string: \6\0#
Value using poi's data formatter: using poi's cleanFormatStr: 60# result: 166609647
Value using my data formatter: using my cleanFormatStr: '60'# result: 60166609647
外观像我的Apache POI错误。你有可能写一个简短的junit单元测试来展示这个问题,然后在Apache POI bugzilla中打开一个bug。 – Gagravarr
@Gagravarr我已经在https://bz.apache.org/bugzilla/show_bug.cgi?id=61638提交了一个请求,有什么需要修改的吗? –