poi使用

本文介绍如何使用Java POI库进行Excel文件的读写操作。包括创建Excel文件、设置单元格格式、写入数据及公式计算等写操作流程;同时涵盖了从Excel中读取数据并解析为Java对象的读操作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用poi操作excel 备忘如下

写操作:

FileOutputStream fos = new FileOutputStream("d:/stock.xls");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
wb.setSheetName(0, "stock");

HSSFCell cell;
int i = 0;

HSSFCellStyle numberCellStyle = wb.createCellStyle();
HSSFDataFormat numberFormat = wb.createDataFormat();
numberCellStyle.setDataFormat(numberFormat.getFormat("0.00"));

HSSFCellStyle profitCellStyle = wb.createCellStyle();
HSSFDataFormat profitFormat = wb.createDataFormat();
profitCellStyle.setDataFormat(profitFormat.getFormat("0.00%"));

for (Iterator it = data1.iterator(); it.hasNext();) {
Map m = (Map) it.next();
HSSFRow row = s.createRow(i++);
String name = (String) m.get("NAME");
cell = row.createCell(0);
cell.setCellValue(name);

String code = (String) m.get("CODE");
cell = row.createCell(1);
cell.setCellValue(code);

BigDecimal importPrice = (BigDecimal) m.get("IMPORT_PRICE");
cell = row.createCell(2);
cell.setCellStyle(numberCellStyle);
cell.setCellValue(importPrice.doubleValue());

BigDecimal closePrice = (BigDecimal) m.get("CLOSE_PRICE");
cell = row.createCell(3);
cell.setCellStyle(numberCellStyle);
cell.setCellValue(closePrice.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());

String profit = (String) m.get("PROFIT");
cell = row.createCell(4);
cell.setCellValue(profit);

cell = row.createCell(5);
cell.setCellFormula(getProfitFomulaByRowNum(row.getRowNum()));
cell.setCellStyle(profitCellStyle);

}
wb.write(fos);
fos.close();


读操作:

FileInputStream fis = new FileInputStream(
"D:/eclipse/workspaces/handworkspace/xyzq/xyzq-data/data/stockpool.xls"); // 根据excel文件路径创建文件流
POIFSFileSystem fs = new POIFSFileSystem(fis); // 利用poi读取excel文件流
HSSFWorkbook wb = new HSSFWorkbook(fs); // 读取excel工作簿
HSSFSheet sheet = wb.getSheetAt(0); // 读取excel的sheet,0表示读取第一个、1表示第二个.....
Map cateMap = cateMap();
// 获取sheet中总共有多少行数据sheet.getPhysicalNumberOfRows()
// log.info("sheet.getPhysicalNumberOfRows()="+sheet.getPhysicalNumberOfRows());
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
HSSFRow row = sheet.getRow(i); // 取出sheet中的某一行数据
if (row != null) {
StringBuffer sb = new StringBuffer();
sb.append(row.getCell(0).getStringCellValue()).append(" ")
.append(row.getCell(1).getStringCellValue())
.append(" ").append(
row.getCell(2).getDateCellValue()).append(
" ").append(
row.getCell(3).getNumericCellValue())
.append(" ").append(
row.getCell(4).getStringCellValue())
.append(" ").append(
row.getCell(5).getStringCellValue())
.append(" ");
System.out.println(sb);
Map param = new HashMap();
param.put("CODE", row.getCell(0).getStringCellValue());
param.put("NAME", row.getCell(1).getStringCellValue());
param.put("IMPORT_TIME", row.getCell(2).getDateCellValue());
param.put("IMPORT_PRICE", new BigDecimal(row.getCell(3)
.getNumericCellValue()).setScale(2,
BigDecimal.ROUND_HALF_UP));
param.put("DESCRIPTION", row.getCell(5)
.getStringCellValue());
param.put("RISK_PROMPT", row.getCell(6)
.getStringCellValue());
param.put("POOL", new Long(STOCK_POOL_TYPE_0));
param.put("CATE_ID", cateMap.get(row.getCell(4)
.getStringCellValue()));
this.insert("TG_STOCK_POOL", param);
}
}
} catch (Exception e) {
e.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

onlydo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值