poi.jar 这个jar包都支持excel哪些版本啊 如题,求用过的人回答,最好能给点小例子用法之类的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // 创建EXCEL文件 private void createExcel(List excelList) { FileOutputStream fos = null; try { String filePath = ServletActionContext.getRequest() .getRealPath("/") + "/" + fileName; File file = new File(filePath); if (!file.exists()) file.getParentFile().mkdirs(); fos = new FileOutputStream(filePath); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); wb.setSheetName(0, "游戏人群统计"); HSSFRow header = sheet.createRow(0); header.createCell(0).setCellValue("序号"); header.createCell(1).setCellValue("分类"); header.createCell(2).setCellValue("子分类"); header.createCell(3).setCellValue("游戏名称"); header.createCell(4).setCellValue("男性玩家数"); header.createCell(5).setCellValue("女性玩家数"); header.createCell(6).setCellValue("运行时间"); if (excelList != null) { for (short i = 0; i < excelList.size(); i++) { GameCount gCount = (GameCount) excelList.get(i); if (gCount != null) { HSSFRow row = sheet.createRow(i + 1); row.createCell(0).setCellValue(i + 1); if (gCount.getCategories() != null) row.createCell(1).setCellValue( gCount.getCategories()); else row.createCell(1).setCellValue(""); if (gCount.getSubCategory() != null) row.createCell(2).setCellValue( gCount.getSubCategory()); else row.createCell(2).setCellValue(""); if (gCount.getGameName() != null) { row.createCell(3) .setCellValue(gCount.getGameName()); } else { row.createCell(3).setCellValue(""); } if (gCount.getMenRunCounts() != null) { row.createCell(4).setCellValue( gCount.getMenRunCounts()); } else { row.createCell(4).setCellValue(""); } if (gCount.getWomenRunCounts() != null) { row.createCell(5).setCellValue( gCount.getWomenRunCounts()); } else { row.createCell(5).setCellValue(""); } if(null != gCount.getRunTime()){ row.createCell(6).setCellValue(sdf.format(gCount.getRunTime())); }else{ row.createCell(6).setCellValue(""); } } } } wb.write(fos); fos.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (null != fos) { fos.close(); } } catch (Exception ex) { } } }这是POI 对excel03版的创建 .xls 还支持07版以后 不过用的就不是这套类了你可以百度下 网上很多例子 不过你要注明什么版本 支持office新版本,可以到官网下载最新包http://poi.apache.org/ public static Map<String, Object> readExcel(File excel) throws Exception { InputStream in = new FileInputStream(excel); Workbook book = null; try { book = new HSSFWorkbook(in); } catch(OutOfMemoryError e) { String msg = "The file " + excel.getName() + " data is error, please delete invalid data!"; if(log.isDebugEnabled()) { log.debug(msg); } throw new Exception(msg); } catch(OfficeXmlFileException e) { in = new FileInputStream(excel); book = new XSSFWorkbook(in); //throw new Exception(e.getMessage()); } if(book == null || book.getNumberOfSheets() == 0) { return null; } for(int sheetNum = 0; sheetNum < book.getNumberOfSheets(); sheetNum++) { Sheet sheet = book.getSheetAt(sheetNum); if(sheet == null || sheet.getLastRowNum() == 0) { return null; } return readExcelFile(sheet); } return null; }public static Map<String, Object> readExcelFile(Sheet sheet) { TitleBean titleBean = new TitleBean(); Map<String, Object> map = new LinkedHashMap<String, Object>(); if(sheet == null || sheet.getLastRowNum() == 0) { return map; } else { map.put("sheetName", sheet.getSheetName()); } for(int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) { Row row = sheet.getRow(rowIndex); if(row == null || row.getLastCellNum() == 0) { continue; } CommonBean bean = new CommonBean(); for(int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) { Cell cell = row.getCell(cellIndex); cell = cell == null ? row.createCell(cellIndex) : cell; String val = ""; switch(cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: val = cell.getStringCellValue().trim(); break; case HSSFCell.CELL_TYPE_NUMERIC: if(HSSFDateUtil.isCellDateFormatted(cell)) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); val = format.format( new Date(cell.getDateCellValue().getTime())); } else { Double d = cell.getNumericCellValue(); //DecimalFormat df = new DecimalFormat(); //df.applyPattern("0"); val = String.valueOf(d); } break; } if(rowIndex == 0) { titleBean.put(val); //titleBean.put(cellIndex, val); if(cellIndex == row.getLastCellNum() - 1) { map.put("title", titleBean); } continue; } if(cellIndex == 0) { bean.setKey(val); } bean.setValue(titleBean.get(cellIndex), val); } if(bean.getKey() != null) { map.put(bean.getKey(), bean); } } return map; } 大家都用什么开发工具? 给我解释一下为什么有这样的输出结果 关于executeBatch的问题 界面动态显示的问题 我刚下了一个jdk,请问如何设置环境变量呢?还有如何编译和运行一个java程序? 继续问个小问题 如何让一个窗体一直获得焦点直到关闭它? 如果不让tomcat显示目录文件列表的问题 Error loading class classname : wrong name, 新手的问题,很简单的,麻烦各位高手指点!:) 怎样统计字符串长度 java.lang.StackOverFlowError 怎么解决 具体点 谢谢
// 创建EXCEL文件
private void createExcel(List excelList) {
FileOutputStream fos = null;
try {
String filePath = ServletActionContext.getRequest()
.getRealPath("/")
+ "/" + fileName;
File file = new File(filePath);
if (!file.exists())
file.getParentFile().mkdirs();
fos = new FileOutputStream(filePath);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, "游戏人群统计");
HSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("序号");
header.createCell(1).setCellValue("分类");
header.createCell(2).setCellValue("子分类");
header.createCell(3).setCellValue("游戏名称");
header.createCell(4).setCellValue("男性玩家数");
header.createCell(5).setCellValue("女性玩家数");
header.createCell(6).setCellValue("运行时间");
if (excelList != null) {
for (short i = 0; i < excelList.size(); i++) {
GameCount gCount = (GameCount) excelList.get(i);
if (gCount != null) {
HSSFRow row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(i + 1);
if (gCount.getCategories() != null)
row.createCell(1).setCellValue(
gCount.getCategories());
else
row.createCell(1).setCellValue("");
if (gCount.getSubCategory() != null)
row.createCell(2).setCellValue(
gCount.getSubCategory());
else
row.createCell(2).setCellValue("");
if (gCount.getGameName() != null) {
row.createCell(3)
.setCellValue(gCount.getGameName());
} else {
row.createCell(3).setCellValue("");
}
if (gCount.getMenRunCounts() != null) {
row.createCell(4).setCellValue(
gCount.getMenRunCounts());
} else {
row.createCell(4).setCellValue("");
}
if (gCount.getWomenRunCounts() != null) {
row.createCell(5).setCellValue(
gCount.getWomenRunCounts());
} else {
row.createCell(5).setCellValue("");
}
if(null != gCount.getRunTime()){
row.createCell(6).setCellValue(sdf.format(gCount.getRunTime()));
}else{
row.createCell(6).setCellValue("");
}
}
}
}
wb.write(fos);
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (null != fos) {
fos.close();
}
} catch (Exception ex) {
}
}
}这是POI 对excel03版的创建 .xls 还支持07版以后 不过用的就不是这套类了
你可以百度下 网上很多例子 不过你要注明什么版本
http://poi.apache.org/
public static Map<String, Object> readExcel(File excel) throws Exception {
InputStream in = new FileInputStream(excel);
Workbook book = null; try {
book = new HSSFWorkbook(in);
}
catch(OutOfMemoryError e) {
String msg = "The file " + excel.getName()
+ " data is error, please delete invalid data!";
if(log.isDebugEnabled()) {
log.debug(msg);
} throw new Exception(msg);
}
catch(OfficeXmlFileException e) {
in = new FileInputStream(excel);
book = new XSSFWorkbook(in);
//throw new Exception(e.getMessage());
} if(book == null || book.getNumberOfSheets() == 0) {
return null;
} for(int sheetNum = 0; sheetNum < book.getNumberOfSheets(); sheetNum++) {
Sheet sheet = book.getSheetAt(sheetNum); if(sheet == null || sheet.getLastRowNum() == 0) {
return null;
} return readExcelFile(sheet);
} return null;
}public static Map<String, Object> readExcelFile(Sheet sheet) {
TitleBean titleBean = new TitleBean();
Map<String, Object> map = new LinkedHashMap<String, Object>(); if(sheet == null || sheet.getLastRowNum() == 0) {
return map;
}
else {
map.put("sheetName", sheet.getSheetName());
} for(int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex); if(row == null || row.getLastCellNum() == 0) {
continue;
} CommonBean bean = new CommonBean(); for(int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) {
Cell cell = row.getCell(cellIndex);
cell = cell == null ? row.createCell(cellIndex) : cell;
String val = ""; switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
val = cell.getStringCellValue().trim();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat format =
new SimpleDateFormat("yyyy-MM-dd");
val = format.format(
new Date(cell.getDateCellValue().getTime()));
}
else {
Double d = cell.getNumericCellValue();
//DecimalFormat df = new DecimalFormat();
//df.applyPattern("0");
val = String.valueOf(d);
} break;
} if(rowIndex == 0) {
titleBean.put(val);
//titleBean.put(cellIndex, val); if(cellIndex == row.getLastCellNum() - 1) {
map.put("title", titleBean);
} continue;
} if(cellIndex == 0) {
bean.setKey(val);
} bean.setValue(titleBean.get(cellIndex), val);
} if(bean.getKey() != null) {
map.put(bean.getKey(), bean);
}
} return map;
}