問題是多維的。
首先,數字格式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提交了一個請求,有什麼需要修改的嗎? –