Java arraylist如何从excel文件读取DDE double值并写入数组?

时间:2014-12-16 09:22:06

标签: java excel apache-poi dde

我有一个带有DDE链接到外部源的excel文件。我试图将这些值读入我的java应用程序。

我正在使用Apache POI库来读取excel。尝试时,我收到以下错误:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a numeric value from a text cell
at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:643)
at org.apache.poi.hssf.usermodel.HSSFCell.getNumericCellValue(HSSFCell.java:668)
at BasketExcelRead.run(BasketExcelRead.java:40)
at Tester.main(Tester.java:7)

我的代码是

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class BasketExcelRead implements Runnable {

    public void run() {

        String inputFile = "someexcel.xls";

        List<String> code = new ArrayList<String>();
        List<Double> bid = new ArrayList<Double>();
        List<Double> share = new ArrayList<Double>();

        try {

            FileInputStream file = new FileInputStream(new File(inputFile));

            HSSFWorkbook workbook = new HSSFWorkbook(file);

            HSSFSheet sheet = workbook.getSheetAt(0);

            int ss = sheet.getPhysicalNumberOfRows();

            for (int i = 0; i < ss; i++) {

                HSSFRow row = sheet.getRow(i);
                HSSFCell cellCode = row.getCell(0);
                HSSFCell cellBid = row.getCell(7);
                HSSFCell cellShare = row.getCell(9);

                code.add(cellCode.getStringCellValue());
                bid.add(cellBid.getNumericCellValue());
                share.add(cellShare.getNumericCellValue());

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("done reading");
        }


        for (int i = 0; i < 30; i++) {
            System.out.println(code.get(i)+" "+bid.get(i)+" "+share.get(i));
        }

    }
}

在bid.add(cellBid.getNumericCellValue())中发生文本/数值冲突的单元格; 此cellBid是excel文件中的DDE链接,显示一些数值

0 个答案:

没有答案