使用XSSF POI Excel空白单元格

时间:2014-04-05 10:47:07

标签: java excel apache-poi xssf

我正在尝试将圆顶数据存储到.xlsx excel文件中。

问题是即使我正在使用row.createcell(),我也无法写入空白单元格。 我得到Null Pointer Excpetion错误,也许是因为行没有初始化。

有没有办法初始化行?  感谢

这是我写的方法

private static void sendOrderSingle(String account, String symbol,
        String securityID, String securityExchange, double price,
        String side, double orderQty, String ordType)
        throws SessionNotFound, IOException {

    quickfix.fix42.NewOrderSingle order = new quickfix.fix42.NewOrderSingle();

    SessionID sessionId = (SessionID) initiator.getSessions().get(0);

    order.set(new TransactTime(new Date(0)));
    order.set(new HandlInst('1'));
    order.set(new Account(account));
    order.set(new Symbol(symbol));
    order.set(new SecurityID(securityID));
    order.set(new SecurityExchange(securityExchange));
    order.set(new Price(price));
    order.set(new OrdType(OrdType.LIMIT));
    if (side.equals("BUY")) {
        order.set(new Side(Side.BUY));
    } else {
        order.set(new Side(Side.SELL));
    }
    order.set(new OrderQty(orderQty));
    order.set(new OrdType(OrdType.LIMIT));
    FileInputStream file = new FileInputStream(new File("Excel/Data.xlsx"));
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheetAt(0);
    XSSFCell cellClOrdIds = null;
    cellClOrdIds = sheet.getRow(1).getCell(18);
    Row row = sheet.getRow((int) cellClOrdIds.getNumericCellValue());
    Cell cell0 = row.getCell(0);
    if (cell0 == null) {
        cell0 = row.createCell(0);
    }
    cell0.setCellType(Cell.CELL_TYPE_STRING);
    cell0.setCellValue(account);
    sheet.getRow(1).getCell(18)
            .setCellValue(Double.parseDouble(cellClOrdIds.toString()) + 1);

    FileOutputStream fos = new FileOutputStream("Excel/Data.xlsx");
    workbook.write(fos);
    fos.close();
    order.set(new ClOrdID(Integer.parseInt(cellClOrdIds.toString())+""));
    Session.sendToTarget(order, sessionId);
}

0 个答案:

没有答案