POI解析EXCEL,导出EXCEL


package com.renesola.datafeed.util;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

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 com.renesola.framework.util.DateUtil;
import com.renesola.framework.util.StringUtil;
import com.renesola.shop.util.ShopUtil;

import com.renesola.framework.util.StringUtil;

public class ExcelUtil {

private static ExcelUtil instance = new ExcelUtil();

public static ExcelUtil getInstance() {
return instance;
}

protected ExcelUtil() {

}

public int getColumnCount(HSSFSheet sheet, int maxRow) {
int col = 0;
try {
if (sheet != null) {
int rowCount = sheet.getLastRowNum();
if (rowCount > maxRow) {
rowCount = maxRow;
}
for (int i = 0; i <= rowCount; i++) {
HSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
if (row.getPhysicalNumberOfCells() > col) {
col = row.getPhysicalNumberOfCells();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return col;
}

public List doRead(String filepath) throws Exception {
List list = new ArrayList();
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filepath));
HSSFSheet sheet = workbook.getSheetAt(0);
int colmunCount = getColumnCount(sheet, 4);
DecimalFormat df = new DecimalFormat("0.##########");
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
int cols=row.getFirstCellNum() + colmunCount;
if (row.getPhysicalNumberOfCells() > cols) {
cols=row.getPhysicalNumberOfCells();
}
if(row.getLastCellNum()<1){
continue;
}
String[] s = new String[cols];
for (int j = row.getFirstCellNum(); j < cols; j++) {
HSSFCell cell = row.getCell((short) j);
if (cell == null) {
s[j] = "";
} else {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
s[j] = df.format(cell.getNumericCellValue());
} else {
s[j] = cell.toString();
}
}
}
list.add(s);
}
return list;
}

public String trimSpace(String txt) {
if (!StringUtil.isEmpty(txt) || "null".equalsIgnoreCase(txt)) {
StringBuffer sb = new StringBuffer();
char t1 = '\n', t2 = '\r', t3 = '\t', t4 = ' ', t5 = ' ';
for (int i = 0; i < txt.length(); i++) {
char c = txt.charAt(i);
if (c == t1 || c == t2 || c == t3 || c == t4) {
sb.append(t5);
} else {
sb.append(c);
}
}
txt = StringUtil.replace(sb.toString(), " ", " ");
return txt;
}
return "";
}

public boolean isEmpty(String[] s) {
if (s == null || s.length < 1) { return true; }
int row = 0;
for (int i = 0; i < s.length; i++) {
if (StringUtil.isEmpty(s[i])) {
++row;
}
}
return row == s.length;
}

public boolean exportExcel(List list, String exportFileName) throws Exception {
if (list == null || list.size() <= 2) {
return false;
} else {
String exportPath = exportFileName;
File file = new File(exportPath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
WritableWorkbook workbook = Workbook.createWorkbook(new File(exportPath));
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
for (int i = 0; i < list.size(); i++) {
String[] contents = list.get(i);
for (int j = 0; j < contents.length; j++) {
String content = contents[j];
if (!StringUtil.isEmpty(content)) {
Label label = new Label(j, i, contents[j]);
sheet.addCell(label);
}
}
}
workbook.write();
workbook.close();
return true;
}
}

public static void main(String[] args) {
ExcelUtil obj = new ExcelUtil();
try {
List list = obj.doRead("d:/Country.xls");
StringUtil.print(list);
list = new ArrayList();
list.add(new String[] { "Region.data_sync", "data_sync" });
list.add(new String[] { "REGION_NAME" });
list.add(new String[] { "Africa" });
list.add(new String[] { "Asia Pacific" });
list.add(new String[] { "Australia" });
list.add(new String[] { "EU" });
list.add(new String[] { "Middle East" });
list.add(new String[] { "North America" });
obj.exportExcel(list, "d:/Region_test.xls");
list = obj.doRead("d:/Region_test.xls");
StringUtil.print(list);
} catch (Exception e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(POI解析EXCEL,导出EXCEL)