前台:
//前台页面
<form action="/service/wxExportTool/inputExce.action" method="post" enctype="multipart/form-data" id="form2">
<input type="file" name="file1" id="file1"/>
<input type="hidden" name="uuid2" id="uuid2" value=""/>
</form>
//导入excel
function queRen_DR(){
claseArt();
coverit();
var form = new FormData(document.getElementById("form2"));
$.ajax({
url:"/service/wxExportTool/inputExce.action",
type:"post",
data:form,
processData:false,
contentType:false,
async: false,
success:function(data){
uncoverit();
var flag = data.flag;
if (flag == '0') {
$("#file1").val("");
$("#haved_SL").val("0");
alert(data.msg);
var res = data.data;
var batchid = res.batchid;
var successCount = res.successCount;
$("#batchid").val(batchid);
$("#successCount").html(successCount);
shows();
}else if(flag=='3'){
var batchid = data.data.batchid;
var htm="<a target=\"_blank\" href=\"/service/wxExportTool/outPuterrorExcel.action?batchid="+batchid+"\">导出失败数据</a>";
$("#outputFail").html(htm);
shows();
alert(data.msg);
} else{
alert(data.msg);
return;
}
},
error:function(e){
alert("导入出错!");
uncoverit();
}
});
}
//转换成excel
Workbook rwb = Workbook.getWorkbook(in);
Sheet rs=rwb.getSheet();//或者rwb.getSheet(0)
int clos=rs.getColumns();//得到所有的列
int rows=rs.getRows();//得到所有的行
for (int i = ; i < rows; i++) {
ItvBatchNew itv = new ItvBatchNew();
for (int j = ; j < clos; j++) {
//第一个是列数,第二个是行数
//默认最左边编号也算一列 所以这里得j++
String username=rs.getCell(j++, i).getContents().trim();//用户名
itv.setUsername(username);
String identNbr=rs.getCell(j++, i).getContents().trim();//身份证号码
itv.setIdentnumber(identNbr);
String broadNbr=rs.getCell(j++, i).getContents().trim();//宽带接入号
itv.setBoradnumber(broadNbr);
String address=rs.getCell(j++, i).getContents().trim();//安装地址
itv.setItvaddress(address);
String userphone=rs.getCell(j++, i).getContents().trim();//联系电话
itv.setUserphone(userphone);
String remark=rs.getCell(j++, i).getContents().trim();//备注
itv.setRemark(remark);
itv.setCreator(creator);
itv.setBatchid(batchid);
}
}
//导出excel
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel");
OutputStream out = response.getOutputStream();
//报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框
//attachment表示以附件方式下载。如果要在页面中打开,则改为 inline
response.setHeader("Content-Disposition", "attachment; filename="+batchid+new String("失败".getBytes("gbk"), "iso8859-1")+"excel.xls");
//创建workbook工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet = workbook.createSheet("第一页");
//设置单元格样式
HSSFCellStyle hssfCellStyle = (HSSFCellStyle) workbook.createCellStyle();
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中显示
hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//纵向居中
try {
//创建行
Row row = sheet.createRow();
//创建单元格
Cell cell = row.createCell();
//设置第一行第一格的值
cell.setCellValue("用户名");
//设置单元格的文本居中显示
cell.setCellStyle(hssfCellStyle);
Cell cell1 = row.createCell();
cell1.setCellValue("身份证号");
cell1.setCellStyle(hssfCellStyle);
Cell cell2 = row.createCell();
cell2.setCellValue("宽带接入号");
cell2.setCellStyle(hssfCellStyle);
Cell cell3 = row.createCell();
cell3.setCellValue("安装地址");
cell3.setCellStyle(hssfCellStyle);
Cell cell4 = row.createCell();
cell4.setCellValue("联系电话");
cell4.setCellStyle(hssfCellStyle);
Cell cell5 = row.createCell();
cell5.setCellValue("备注");
cell5.setCellStyle(hssfCellStyle);
Cell cell6 = row.createCell();
cell6.setCellValue("失败原因");
cell6.setCellStyle(hssfCellStyle);
for (int i = ; i < list.size(); i++) {
//创建行
Row rows = sheet.createRow(i+);
//创建单元格
Cell mapcell = rows.createCell();
//设置第一行第一格的值
mapcell.setCellValue(list.get(i).get("USERNAME"));
//设置单元格的文本居中显示
mapcell.setCellStyle(hssfCellStyle);
Cell mapcell1 = rows.createCell();
mapcell1.setCellValue(list.get(i).get("IDENTNUMBER"));
mapcell1.setCellStyle(hssfCellStyle);
Cell mapcell2 = rows.createCell();
mapcell2.setCellValue(list.get(i).get("BROADNUMBER"));
mapcell2.setCellStyle(hssfCellStyle);
Cell mapcell3 = rows.createCell();
mapcell3.setCellValue(list.get(i).get("ADDRESS"));
mapcell3.setCellStyle(hssfCellStyle);
Cell mapcell4 = rows.createCell();
mapcell4.setCellValue(list.get(i).get("USERPHONE"));
mapcell4.setCellStyle(hssfCellStyle);
Cell mapcell5 = rows.createCell();
mapcell5.setCellValue(list.get(i).get("REMARK"));
mapcell5.setCellStyle(hssfCellStyle);
Cell mapcell6 = rows.createCell();
mapcell6.setCellValue(list.get(i).get("REASON"));
mapcell6.setCellStyle(hssfCellStyle);
}
workbook.write(out);
// System.out.println("数据写入成功!");
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}