使用POI操作Excel修改模板(批量替換excel中的資料並判斷excel版本)
package com.sycamore.controller;
import org.apache.poi.POIXMLDocument;
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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.sycamore.util.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Created by sunming on 2017/12/13.
*/
public class ExcelUtils {
/**
* 替換Excel2003版模板檔案內容
* @param item 文件資料
* @param sourceFilePath Excel模板檔案路徑
* @param targetFilePath Excel生成檔案路徑
*/
public static boolean replaceModel2003(Map item, String sourceFilePath, String targetFilePath) {
boolean bool = true;
try {
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while(rows.hasNext()){
if(row!=null) {
int num = row.getLastCellNum();
for(int i=0;i<num;i++) {
XSSFCell cell= row.getCell(i);
if(cell!=null) {
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
}
if(cell==null || cell.getStringCellValue()==null) {
continue;
}
String value= cell.getStringCellValue();
if(!"".equals(value)) {
Set<String> keySet = item.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String text = it.next();
if(value.equalsIgnoreCase(text)) {
cell.setCellValue((String)item.get(text));
break;
}
}
} else {
cell.setCellValue("");
}
}
}
}
// 輸出檔案
FileOutputStream fileOut = new FileOutputStream(targetFilePath);
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
bool = false;
e.printStackTrace();
}
return bool;
}
/**
* 替換Excel2007版模板檔案內容
* @param item 文件資料
* @param sourceFilePath Excel模板檔案路徑
* @param targetFilePath Excel生成檔案路徑
*/
public static boolean replaceModel2007(Map item, String sourceFilePath, String targetFilePath) {
boolean bool = true;
try {
InputStream is = new FileInputStream(sourceFilePath);
XSSFWorkbook wb = new XSSFWorkbook(is);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while(rows.hasNext()){
XSSFRow row = (XSSFRow) rows.next();
if(row!=null) {
int num = row.getLastCellNum();
for(int i=0;i<num;i++) {
XSSFCell cell = row.getCell(i);
if(cell!=null) {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
}
if(cell==null || cell.getStringCellValue()==null) {
continue;
}
String value= cell.getStringCellValue();
if(!"".equals(value)) {
Set<String> keySet = item.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String text = it.next();
if(value.equalsIgnoreCase(text)) {
cell.setCellValue((String)item.get(text));
break;
}
}
} else {
cell.setCellValue("");
}
}
}
}
// 輸出檔案
FileOutputStream fileOut = new FileOutputStream(targetFilePath);
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
bool = false;
e.printStackTrace();
}
return bool;
}
/**
* 判斷Excel版本
* */
public static void replaceModel(String filePath,Map<String,Object> dataSource,String targetFilePath) throws Exception{
/**
*判斷excel版本是2003還是2007版
* */
InputStream inp = new FileInputStream(new File(filePath));
if(!inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
if(POIFSFileSystem.hasPOIFSHeader(inp)) {
LogUtil.info("進入2003版excel....");
replaceModel2003(dataSource, filePath, targetFilePath);
}
if(POIXMLDocument.hasOOXMLHeader(inp)) {
LogUtil.info("進入2007版excel....");
replaceModel2007(dataSource, filePath, targetFilePath);
}
}
// 測試
public static void main(String[] args) throws Exception {
Map item = new HashMap();
item.put("L-00001","L-00012");
item.put("L-00002","L-00013");
item.put("L-00003","L-00014");
String path = "C:\\Users\\sunming\\Desktop\\22\\22.xls";
String path2 = "C:\\Users\\sunming\\Desktop\\22\\33.xls";
replaceModel(path, item, path2);
}
}
連結:http://blog.csdn.net/Code_KK/article/details/78799306