简体   繁体   English

如何使用Java在Excel中输入无效公式

[英]how to input invalid formula in Excel using Java

I'm currently making a code that creates an Excel using Java jxl. 我目前正在编写使用Java jxl创建Excel的代码。 I've encountered no problem as long as the formula I'm using exists. 只要我使用的公式存在,我就不会遇到任何问题。 The problem is, I need to input a formula which excel will not be able to find because we use a special excel to read it. 问题是,我需要输入一个excel无法找到的公式,因为我们使用特殊的excel来读取它。 I need my code to retain the invalid formula instead of writing =1 ERROR() on the cell so that when my file is opened using our special excel, it gets the value. 我需要我的代码保留无效的公式,而不是在单元格上写入= 1 ERROR(),以便使用特殊的excel打开文件时,它会获取值。 How do I go about this? 我该怎么办?

Here is my code: 这是我的代码:

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);

/** The value in the bid = "BDP("EURON Curncy","PX_BID","DATA") 1"; / **出价中的值=“ BDP(” EURON Curency“,” PX_BID“,” DATA“) 1”; * / * /

                f = new Formula(6,nRow,bid,new WritableCellFormat(dp2));
                s.addCell(f); 

/** The value in the offer = "BDP("EURON Curncy","PX_ASK","DATA") 1"; / **报价中的值=“ BDP(” EURON Curency“,” 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();

I supose that your "special excel" is that you have a plug in with visual basic functions to use whithin cell formulas 我认为您的“特别擅长”是您拥有一个具有可视基本功能的插件,可以使用whithin单元格公式

I dont know if the current version of JXL support macros or at least don't delete it when you modify an existent Workbook, but in your code your are not updating an existing Workbook, your are creating a new one 我不知道当前的JXL支持宏版本还是至少在修改现有工作簿时不删除它,但是在代码中您没有更新现有工作簿,而是在创建新的工作簿。

have you tried to use your code opening an existing workbook in which the visual basic function works? 您是否尝试过使用代码打开现有的工作簿,而该工作簿在其中可以使用Visual Basic功能? (it can be an empty one). (可以是一个空的)。 If it works, you can use that file as a template, copy it to the location you want, rename it, and edit it with JXL 如果可行,您可以将该文件用作模板,将其复制到所需的位置,重命名并使用JXL进行编辑

Sorry, I only have time for a quick reply, but have you looked at Apache POI at all? 抱歉,我只想快速回复一下,但是您是否看过Apache POI Its API might be of use to you. 它的API可能对您有用。

However, I'm not 100% sure this is for writing Excel as I think its primary use is reading MS Office files. 但是,我不确定100%是否用于编写Excel,因为我认为它的主要用途是读取MS Office文件。 Still, might be worth a look though. 不过,也许值得一看。

The problem is, I need to input a formula which excel will not be able to find because we use a special excel to read it. 问题是,我需要输入一个excel无法找到的公式,因为我们使用特殊的excel来读取它。 I need my code to retain the invalid formula instead of writing =1 ERROR() on the cell so that when my file is opened using our special excel, it gets the value. 我需要我的代码保留无效的公式,而不是在单元格上写入= 1 ERROR(),以便使用特殊的excel打开文件时,它会获取值。

Your problem isn't JExcel or POI. 您的问题不是JExcel或POI。 No API will be able to help you with a formula that it cannot be found. 没有API可以帮助您解决无法找到的公式。

What is this "special excel"? 这是什么“特别擅长”? If it's another file, simply read it in and use it. 如果是另一个文件,只需读入并使用它即可。

Your question is confusing. 您的问题令人困惑。 I'm having a hard time understanding what exactly is going on. 我很难理解到底发生了什么。 I'm voting to close unless you can clarify. 除非您无法澄清,否则我投票关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM