Excel工具类ExcelUtil.java源码:
-
package com.itjh.javaUtil;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
import java.text.DecimalFormat;
-
import java.util.LinkedList;
-
import java.util.List;
-
-
import javax.servlet.http.HttpServletResponse;
-
-
import org.apache.poi.hssf.usermodel.HSSFCell;
-
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
-
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.openxml4j.exceptions.InvalidFormatException;
-
import org.apache.poi.ss.usermodel.Cell;
-
import org.apache.poi.ss.usermodel.DateUtil;
-
import org.apache.poi.ss.usermodel.Row;
-
import org.apache.poi.ss.usermodel.Sheet;
-
import org.apache.poi.ss.usermodel.Workbook;
-
import org.apache.poi.ss.usermodel.WorkbookFactory;
-
-
-
-
-
-
-
-
-
-
-
public class ExcelUtil {
-
-
-
private String path = "";
-
-
-
private boolean autoColumnWidth = false;
-
-
-
-
-
public ExcelUtil() {
-
}
-
-
-
-
-
-
-
-
public ExcelUtil(String path) {
-
this.path = path;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public List<Object[]> read(int sheetOrder) throws FileNotFoundException,
-
IOException, InvalidFormatException {
-
FileInputStream fis = new FileInputStream(path);
-
Workbook workbook = WorkbookFactory.create(fis);
-
if (fis != null) {
-
fis.close();
-
}
-
Sheet sheet = workbook.getSheetAt(sheetOrder);
-
-
List<Object[]> valueList = new LinkedList<Object[]>();
-
-
for (Row row : sheet) {
-
-
Object[] rowObject = null;
-
for (Cell cell : row) {
-
-
switch (cell.getCellType()) {
-
case Cell.CELL_TYPE_BOOLEAN:
-
-
rowObject = CollectionUtil.addObjectToArray(rowObject,
-
cell.getBooleanCellValue());
-
break;
-
case Cell.CELL_TYPE_NUMERIC:
-
-
if (DateUtil.isCellDateFormatted(cell)) {
-
-
rowObject = CollectionUtil.addObjectToArray(rowObject,
-
cell.getDateCellValue());
-
} else {
-
DecimalFormat df = new DecimalFormat();
-
-
String value = df.format(cell.getNumericCellValue())
-
.replace(",", "");
-
-
rowObject = CollectionUtil.addObjectToArray(rowObject,
-
value);
-
}
-
break;
-
case Cell.CELL_TYPE_FORMULA:
-
-
rowObject = CollectionUtil.addObjectToArray(rowObject,
-
cell.getCellFormula());
-
break;
-
case Cell.CELL_TYPE_STRING:
-
-
rowObject = CollectionUtil.addObjectToArray(rowObject, cell
-
.getRichStringCellValue().toString());
-
break;
-
}
-
}
-
-
valueList.add(rowObject);
-
}
-
return valueList;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public String read(int sheetOrder, int colum, int row) throws Exception {
-
FileInputStream fis = new FileInputStream(path);
-
Workbook workbook = WorkbookFactory.create(fis);
-
if (fis != null) {
-
fis.close();
-
}
-
Sheet sheet = workbook.getSheetAt(sheetOrder);
-
Row rows = sheet.getRow(row - 1);
-
Cell cell = rows.getCell(colum - 1);
-
String content = cell.getStringCellValue();
-
return content;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public void write(int sheetOrder, int colum, int row, String content)
-
throws Exception {
-
FileInputStream fis = new FileInputStream(path);
-
Workbook workbook = WorkbookFactory.create(fis);
-
if (fis != null) {
-
fis.close();
-
}
-
Sheet sheet = workbook.getSheetAt(sheetOrder);
-
Row rows = sheet.createRow(row - 1);
-
Cell cell = rows.createCell(colum - 1);
-
cell.setCellValue(content);
-
FileOutputStream fileOut = new FileOutputStream(path);
-
workbook.write(fileOut);
-
fileOut.close();
-
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public int getSheetLastRowNum(int sheetOrder) throws IOException,
-
InvalidFormatException {
-
FileInputStream fis = new FileInputStream(path);
-
Workbook workbook = WorkbookFactory.create(fis);
-
if (fis != null) {
-
fis.close();
-
}
-
Sheet sheet = workbook.getSheetAt(sheetOrder);
-
return sheet.getLastRowNum();
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public void makeExcel(String sheetName, String[] fieldName,
-
List<Object[]> data) throws IOException {
-
-
HSSFWorkbook workbook = makeWorkBook(sheetName, fieldName, data);
-
-
String filePath = path.substring(0, path.lastIndexOf("\\"));
-
-
File file = new File(filePath);
-
-
if (!file.exists())
-
file.mkdirs();
-
FileOutputStream fileOut = new FileOutputStream(path);
-
workbook.write(fileOut);
-
fileOut.close();
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public void makeStreamExcel(String excelName, String sheetName,
-
String[] fieldName, List<Object[]> data,
-
HttpServletResponse response) throws IOException {
-
OutputStream os = null;
-
response.reset();
-
os = response.getOutputStream();
-
response.setHeader("Content-disposition", "attachment; filename="
-
+ new String(excelName.getBytes(), "ISO-8859-1"));
-
response.setContentType("application/msexcel");
-
-
HSSFWorkbook workbook = makeWorkBook(sheetName, fieldName, data);
-
os.flush();
-
workbook.write(os);
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private HSSFWorkbook makeWorkBook(String sheetName, String[] fieldName,
-
List<Object[]> data) {
-
-
Integer collength[] = new Integer[fieldName.length];
-
-
-
HSSFWorkbook workbook = new HSSFWorkbook();
-
-
HSSFSheet sheet = workbook.createSheet();
-
-
workbook.setSheetName(0, sheetName);
-
-
HSSFRow row = sheet.createRow(0);
-
-
HSSFCell cell;
-
-
for (int i = 0; i < fieldName.length; i++) {
-
-
cell = row.createCell((short) i);
-
-
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
-
-
-
-
cell.setCellValue(new HSSFRichTextString(fieldName[i]));
-
-
collength[i] = fieldName[i].getBytes().length;
-
}
-
-
String tempCellContent = "";
-
-
for (int i = 0; i < data.size(); i++) {
-
Object[] tmp = data.get(i);
-
-
row = sheet.createRow(i + 1);
-
for (int j = 0; j < tmp.length; j++) {
-
cell = row.createCell((short) j);
-
-
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
-
tempCellContent = (tmp[j] == null) ? "" : tmp[j].toString();
-
cell.setCellValue(new HSSFRichTextString(tempCellContent));
-
-
-
if (autoColumnWidth) {
-
if (j >= collength.length) {
-
collength = CollectionUtil.addObjectToArray(collength,
-
tempCellContent.getBytes().length);
-
} else {
-
-
if (collength[j] < tempCellContent.getBytes().length) {
-
collength[j] = tempCellContent.getBytes().length;
-
}
-
}
-
}
-
}
-
}
-
-
-
if (autoColumnWidth) {
-
-
for (int i = 0; i < fieldName.length; i++) {
-
sheet.setColumnWidth(i, collength[i] * 2 * 256);
-
}
-
}
-
return workbook;
-
}
-
-
-
-
-
-
-
-
-
-
public void setAutoColumnWidth(boolean autoColumnWidth) {
-
this.autoColumnWidth = autoColumnWidth;
-
}
-
}