我目前正在創建一個使用Java jxl創建Excel的代碼。只要我使用的公式存在,我就沒有遇到任何問題。問題是,我需要輸入一個excel無法找到的公式,因爲我們使用特殊的excel來讀取它。我需要我的代碼保留無效的公式,而不是在單元格中寫入= 1 ERROR(),以便在使用我們特殊的Excel打開我的文件時,它會獲取值。我如何去做這件事?如何在Excel中使用Java輸入無效公式
這是我的代碼:
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
workbook = Workbook.createWorkbook(file, ws);
WritableSheet s = workbook.createSheet("Sheet1", 0);
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);
DateFormat dateFormat = new DateFormat("M/d/yyyy H:mm");
NumberFormat dp = new NumberFormat("#");
NumberFormat dp2 = new NumberFormat("#.##");
ResultSetMetaData rsmd = rs.getMetaData();
int nRow = 0;
Label l = null;
Formula f = null;
for(int index = 1; index <= rsmd.getColumnCount(); index++) {
l = new Label(index-1,nRow,rsmd.getColumnName(index),new WritableCellFormat(wf));
s.addCell(l);
}
nRow++;
while(rs.next()) {
String valueDate = VerifyNull.ifNull(rs.getString(1), Constant.BLANK);
f = new Formula(0,nRow,valueDate,new WritableCellFormat(dp));
s.addCell(f);
String rateDate = VerifyNull.ifNull(rs.getString(2), Constant.BLANK);
f = new Formula(1,nRow,rateDate,new WritableCellFormat(dp));
s.addCell(f);
String market = VerifyNull.ifNull(rs.getString(3), Constant.BLANK);
l = new Label(3,nRow,market,new WritableCellFormat(wf));
s.addCell(l);
String ccy = VerifyNull.ifNull(rs.getString(4), Constant.BLANK);
l = new Label(4,nRow,ccy,new WritableCellFormat(wf));
s.addCell(l);
String label = VerifyNull.ifNull(rs.getString(5), Constant.BLANK);
l = new Label(5,nRow,label,new WritableCellFormat(wf));
s.addCell(l);
String quoteDate = VerifyNull.ifNull(rs.getString(6), Constant.BLANK);
f = new Formula(1,nRow,quoteDate,new WritableCellFormat(dp));
s.addCell(f);
String bid = "="+VerifyNull.ifNull(rs.getString(7), Constant.BLANK);
/**在出價= 「BDP(」 EURON Curncy」, 「PX_BID」, 「DATA」)1" 的值; */
f = new Formula(6,nRow,bid,new WritableCellFormat(dp2));
s.addCell(f);
/**在要約= 「BDP(」 EURON Curncy」, 「PX_ASK」, 「DATA」)1" 的值; */
String offer = "="+VerifyNull.ifNull(rs.getString(8), Constant.BLANK);
f = new Formula(7,nRow,offer,new WritableCellFormat(dp2));
s.addCell(f);
String timeStamp = VerifyNull.ifNull(rs.getString(9), Constant.BLANK);
f = new Formula(8,nRow,timeStamp,new WritableCellFormat(dateFormat));
s.addCell(f);
nRow++;
}
workbook.write();
workbook.close();