代码块
package com.yunlu.mimas.sasac.utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
import java.util.Map;
public class Excel2003Utils {
protected HSSFWorkbook hssfWorkbook;
private String fullPathName;
public Excel2003Utils(String fullPathName) throws Exception {
this.fullPathName = fullPathName;
this.hssfWorkbook = new HSSFWorkbook(new FileInputStream(new File(fullPathName)));
}
private void setCellValueByAlias(String alias, String value) {
Name name = hssfWorkbook.getName(alias);
if (name != null) {
CellReference cellReference = new CellReference(name.getRefersToFormula());
Sheet sheet = hssfWorkbook.getSheet(cellReference.getSheetName());
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
cell.setCellValue(value);
}
}
/**
* 通过Map<String,String> 给excel 设值
*
* @param values
* @param outputStream
* @throws Exception
*/
public void setValues(Map<String, String> values, OutputStream outputStream) throws Exception {
for (String key : values.keySet()) {
try {
setCellValueByAlias(key, values.get(key));
}catch (Exception e){
e.printStackTrace();
}
}
hssfWorkbook.write(outputStream);
}
}