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<String[]> doRead(String filepath) throws Exception {
List<String[]> list = new ArrayList<String[]>();
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<String[]> 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<String[]> list = obj.doRead("d:/Country.xls");
StringUtil.print(list);
list = new ArrayList<String[]>();
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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值