您的位置:首页 > 编程语言 > Java开发

javapoi读写数据 excel

2013-07-07 01:30 393 查看
针对2007的excel版本XSSFCELL,其他版本HSSFCELL


public HSSFWorkbook exportOrderWeek(String startDate,String endDate,String exportType){
List<Object[]> orderWeekList = dataInfoDao.orderWeekData(startDate,endDate,exportType);

// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("统计结果");
//设置列宽

// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
sheet.setColumnWidth(0, 2500);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 5000);
sheet.setColumnWidth(3, 6000);
sheet.setColumnWidth(4, 2500);
sheet.setColumnWidth(5, 2500);
sheet.setColumnWidth(6, 5000);
sheet.setColumnWidth(7, 2500);
sheet.setColumnWidth(8, 5000);
sheet.setColumnWidth(9, 2500);
sheet.setColumnWidth(10,5000);
sheet.setColumnWidth(11, 5000);
sheet.setColumnWidth(12, 5000);
sheet.setColumnWidth(13, 5000);
sheet.setColumnWidth(14, 5000);
sheet.setColumnWidth(15, 5000);
sheet.setColumnWidth(16, 7000);
//普通行样式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

//第一行样式
HSSFCellStyle rowStyle = wb.createCellStyle();
rowStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
rowStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
rowStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
rowStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
rowStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
//粗体显示
HSSFFont rowFont = wb.createFont();
rowFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
rowStyle.setFont(rowFont);

if(orderWeekList != null && orderWeekList.size()>0){

HSSFCell cell = row.createCell(0);
cell.setCellValue("订单");
cell.setCellStyle(rowStyle);
cell = row.createCell(1);
cell.setCellValue("订单号");
cell.setCellStyle(rowStyle);
cell = row.createCell(2);
cell.setCellValue("客户昵称");
cell.setCellStyle(rowStyle);
cell = row.createCell(3);
cell.setCellValue("下单时间");
cell.setCellStyle(rowStyle);
cell = row.createCell(4);
cell.setCellValue("订单状态");
cell.setCellStyle(rowStyle);
cell = row.createCell(5);
cell.setCellValue("订单金额");
cell.setCellStyle(rowStyle);
cell = row.createCell(6);
cell.setCellValue("支付时间");
cell.setCellStyle(rowStyle);
cell = row.createCell(7);
cell.setCellValue("收件人");
cell.setCellStyle(rowStyle);
cell = row.createCell(8);
cell.setCellValue("电话");
cell.setCellStyle(rowStyle);
cell = row.createCell(9);
cell.setCellValue("购物状态");
cell.setCellStyle(rowStyle);
cell = row.createCell(10);
cell.setCellValue("收货地址(国家)");
cell.setCellStyle(rowStyle);
cell = row.createCell(11);
cell.setCellValue("收货地址(省)");
cell.setCellStyle(rowStyle);
cell = row.createCell(12);
cell.setCellValue("收货地址(市)");
cell.setCellStyle(rowStyle);
cell = row.createCell(13);
cell.setCellValue("收货地址(县)");
cell.setCellStyle(rowStyle);
cell = row.createCell(14);
cell.setCellValue("收货地址(街道)");
cell.setCellStyle(rowStyle);
cell = row.createCell(15);
cell.setCellValue("支付宝帐号");
cell.setCellStyle(rowStyle);
cell = row.createCell(16);
//            cell.setCellValue("收货地址");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(12);
//            cell.setCellValue("收货地址");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(13);
//            cell.setCellValue("订单号");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(1);
//            cell.setCellValue("旧订单号");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(2);
//            cell.setCellValue("订单状态");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(3);
//            cell.setCellValue("下单时间");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(4);
//            cell.setCellValue("订单金额");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(5);
//            cell.setCellValue("单品件数");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(6);
//            cell.setCellValue("原价");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(7);
//            cell.setCellValue("现价");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(8);
//            cell.setCellValue("类别");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(9);
//            cell.setCellValue("品牌");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(10);
//            cell.setCellValue("尺寸");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(11);
//            cell.setCellValue("性别");
//            cell.setCellStyle(rowStyle);
//            cell = row.createCell(12);
//            cell.setCellValue("用户昵称");
//            cell.setCellStyle(rowStyle);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
for (int i = 0; i < orderWeekList.size(); i++){
row = sheet.createRow(i + 1);
// 第四步,创建单元格,并设置值
cell = row.createCell(0);
cell.setCellValue(orderWeekList.get(i)[0] == null ? "" : orderWeekList.get(i)[0].toString());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(orderWeekList.get(i)[1] == null ? "" : orderWeekList.get(i)[1].toString());
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(orderWeekList.get(i)[2] == null ? "" : orderWeekList.get(i)[2].toString());
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue(orderWeekList.get(i)[3] == null ? "" : orderWeekList.get(i)[3].toString());
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue(orderWeekList.get(i)[4] == null ? "" : orderWeekList.get(i)[4].toString());
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue(orderWeekList.get(i)[5] == null ? "" : orderWeekList.get(i)[5].toString());
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue(orderWeekList.get(i)[6] == null ? "" : orderWeekList.get(i)[6].toString());
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue(orderWeekList.get(i)[7] == null ? "" : orderWeekList.get(i)[7].toString());
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue(orderWeekList.get(i)[8] == null ? "" : orderWeekList.get(i)[8].toString());
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue(orderWeekList.get(i)[9] == null ? "" : orderWeekList.get(i)[9].toString());
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue(orderWeekList.get(i)[10] == null ? "" : orderWeekList.get(i)[10].toString());
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue(orderWeekList.get(i)[11] == null ? "" : orderWeekList.get(i)[11].toString());
cell.setCellStyle(style);
cell = row.createCell(12);
cell.setCellValue(orderWeekList.get(i)[12] == null ? "" : orderWeekList.get(i)[12].toString());
cell.setCellStyle(style);
cell = row.createCell(13);
cell.setCellValue(orderWeekList.get(i)[13] == null ? "" : orderWeekList.get(i)[13].toString());
cell.setCellStyle(style);
cell = row.createCell(14);
cell.setCellValue(orderWeekList.get(i)[14] == null ? "" : orderWeekList.get(i)[14].toString());
cell.setCellStyle(style);

cell = row.createCell(15);
cell.setCellValue(orderWeekList.get(i)[15] == null ? "" : orderWeekList.get(i)[15].toString());
cell.setCellStyle(style);

//                cell.setCellValue(orderWeekList.get(i)[10] == null ? "" : orderWeekList.get(i)[10].toString());
//                cell.setCellStyle(style);
//                cell = row.createCell(11);
//                cell.setCellValue(orderWeekList.get(i)[11] == null ? "" : orderWeekList.get(i)[11].toString());
//                cell.setCellStyle(style);
//                cell = row.createCell(12);
//                cell.setCellValue(orderWeekList.get(i)[12] == null ? "" : orderWeekList.get(i)[12].toString());
//                cell.setCellStyle(style);
}
}
}
解析的话,如果需要上传添加到spring.xml,这和controller方法里的参数一致(@RequestParam("upload") CommonsMultipartFile files)
<!--  文件上传支持 begin -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000000" />
</bean>
jsp:
<form action="${ctx}/account/xyyy/add" method="post" enctype="multipart/form-data">
导入卡:<input type="file" name="upload" id="exportFile"/>

<input type="submit" value="确定"/> <!-- onclick="javascript:lookfile()" -->
</form>
public class XlsMain {

public static void main( String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();

xlsMain.readXls();
}

private void readXls() throws IOException{
InputStream is = new FileInputStream( "C:\\Users\\Mao\\Desktop\\1_1_spot_order.xlsx");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook( is);

// 循环工作表Sheet
for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt( numSheet);
if(hssfSheet == null){
continue;
}

// 循环行Row
for(int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++){
HSSFRow hssfRow = hssfSheet.getRow( rowNum);
if(hssfRow == null){
continue;
}

// 循环列Cell
for(int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++){
HSSFCell hssfCell = hssfRow.getCell( cellNum);
if(hssfCell == null){
continue;
}

System.out.print("    " + getValue( hssfCell));
}
System.out.println();
}
}
}

@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell){
if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN){
return String.valueOf( hssfCell.getBooleanCellValue());
}else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){
return String.valueOf( hssfCell.getNumericCellValue());
}else{
return String.valueOf( hssfCell.getStringCellValue());
}
}

}
日期类型转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(0) ;
date = HSSFDateUtil.getJavaDate( new Double(getValue(xssfCell)));






                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: