2010-11-02 44 views
1

我目前正在創建一個使用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(); 

回答

0

對不起,我只有時間能夠快速回復,但你看着Apache POI呢?它的API可能對你有用。

但是,我不是100%確定這是寫Excel的,因爲我認爲它的主要用途是讀取MS Office文件。不過,可能值得一看。

0

問題是,我需要輸入一個公式,其中excel將無法找到,因爲我們使用特殊的excel來讀取它。我需要我的代碼保留無效的公式,而不是在單元格中寫入= 1 ERROR(),以便在使用我們特殊的Excel打開我的文件時,它會獲取值。

您的問題不是JExcel或POI。沒有API能夠幫助你找到一個無法找到的公式。

什麼是「特殊excel」?如果是另一個文件,只需閱讀並使用它。

你的問題很混亂。我很難理解究竟發生了什麼。除非你能澄清,否則我投票結束。

1

我supose你的「特殊EXCEL」是,你有一個插件使用Visual Basic功能whithin單元格公式

我不知道如果JXL支持宏的當前版本,或至少不刪除使用它修改現有的工作簿時,但在您的代碼中,您不更新現有的工作簿,您正在創建一個新的工作簿

您是否嘗試過使用代碼打開一個現有的工作簿,其中的Visual Basic函數工作? (它可以是空的)。如果有效,您可以將該文件用作模板,將其複製到您想要的位置,重新命名並使用JXL編輯它

相關問題