POI相关Excel文件读写存问题的解决
以前也使用过POI读写Excel过。对涉及到Excel的POI的几个常用对象还是了解。
不过这次的一个需求是,读取既存的Excel后增删改内容后在进行保存。调查了很久相应API
以及网络资源,没有找到相应的方法。也许是没有找到吧。(以前要么只是new空白文件写,要么只是既存文件读)
不过上面的需求其实是一个很常见的需求,类似这样的需求,如果是PDF的话原路径save一下就OK。
为什么现在就不能原路径save呢。作为一个API调用工程师,确实不知怎么回事。这里也探讨这个问题。只是谈谈我的
解决方法,仅此而已。
我的解决方法与参照集合工具类一个线程安全的类的做法。copyOnWriteArrayList 写时拷贝。这个类很重要,bat面试题哟...
意思就是先读取Excel内容,读取后修改内容,然后将此Excel内容保存在一个临时文件,文件类型不重要。然后在将原来的Excel
删除,再将这个饱含修改内容的文件重新写一个以原名称的为名的新的Excel中,然后删除这个临时文件。这样感觉就是读写时就保存了。
public static Map<Integer, String> readExcelCol(String excelFile, int readCol_B, int readCol_C, int readCol_I, int startRow) {
Map<Integer, String> map = new HashMap<>();
Workbook workbook = null;
try {
workbook = getWorkbook(excelFile);
Sheet sheet = workbook.getSheetAt(0);
int rowCnt = sheet.getLastRowNum();
for (int i = startRow - 1; i <= rowCnt; i++) {// start row
Row row = sheet.getRow(i);
if (row.getZeroHeight()) {
LOG.info(" skip hidden row:" + i);
continue;
}
if (row != null) {
String aValuae = getCellVal(row.getCell(readCol_B));
String bVbalue = getCellVal(row.getCell(readCol_C));
String cValuce = getCellVal(row.getCell(readCol_I));
if ("".equals(aValuae ) || "".equals(bVbalue )) {
continue;
}
map.put(i, aValuae + "_" + bVbalue + "_" + cValuce );
}
}
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (workbook != null) {
workbook.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
public static void writeExcelCol(String excelFile, int rowNum, int col, String value) {
Workbook workbook = null;
try {
workbook = getWorkbook(excelFile);//读取原文件
Sheet sheet = workbook.getSheetAt(0);
int rowCnt = sheet.getLastRowNum();
for (int i = rowNum - 1; i <= rowCnt; i++) {// start row
if (rowNum != i) {
continue;
}
Row row = sheet.getRow(rowNum);
if (row != null) {
row.createCell(col).setCellValue(value);//原Excel中设置值
break;
}
}
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\xxxx\\Desktop\\tmp.xlsx"));//写入临时文件
workbook.write(fileOutputStream);
fileOutputStream.close();
workbook.close();
if (new File(excelFile).exists()) {//如果原来文件存在就删除
new File(excelFile).delete();
}
FileUtil.copy(new FileInputStream(new File("C:\\Users\\xxxx\\Desktop\\tmp.xlsx")), new FileOutputStream(new File(excelFile)));//将临时文件重新命名
if (new File("C:\\Users\\xxxx\\Desktop\\tmp.xlsx").exists()) {如果临时文件存在就删除
new File("C:\\Users\\xxxx\\Desktop\\tmp.xlsx").delete();
}
} catch (Exception e) {
LOG.error(e);
e.printStackTrace();
} finally {
try {
if (workbook != null) {
workbook.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getCellVal(Cell cell) {
String cellVal = "";
if (cell == null) {
return cellVal;
}
cell.setCellType(CellType.STRING);
return cell.getStringCellValue().trim();
}
private static Workbook getWorkbook(String file) {
if (file.endsWith("xlsx")) {
try {
Workbook workbook = new XSSFWorkbook(file);
return workbook;
} catch (IOException e) {
e.printStackTrace();
}
} else if (file.endsWith("xls")) {
try {
return new HSSFWorkbook(new FileInputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void copy(InputStream is, OutputStream os) {
is = new BufferedInputStream(is);
os = new BufferedOutputStream(os);
byte[] b = new byte[1024];
try {
do {
int size = is.read(b);
if (size == 0) {
Thread.yield();
continue;
} else if (size < 0) {
break;
}
os.write(b, 0, size);
} while (true);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

浙公网安备 33010602011771号