Excel导出有问题,但是我是第一次弄这个所以不晓得怎么解决。
Excel模板如果不加所以边框导出来的数据正常如下图:
但是如果在Excel 里加了边框如图:导出来的数据第一个数据位置就错位了,如图:
Java后台,Service层代码如下:
public static void excelImportDatasss(String filurl, Map list, HttpServletResponse response,
String begindate,String enddate,String organizeid,String key) throws IOException {
File excel2 = new File(filurl);
FileInputStream is = new FileInputStream(excel2);
HSSFWorkbook excel = new HSSFWorkbook(is);
Sheet sheet = excel.getSheetAt(0);
Object obj = null;
Row rowa = sheet.getRow(1);
if (rowa == null) {
rowa = sheet.createRow(1);
}
Cell tempcells = rowa.getCell(5);
if (tempcells == null) {
tempcells = rowa.createCell(5);
}
tempcells.setCellValue(begindate+"-"+enddate);
rowa = sheet.getRow(1);
if (rowa == null) {
rowa = sheet.createRow(1);
}
tempcells = rowa.getCell(1);
if (tempcells == null) {
tempcells = rowa.createCell(1);
}
tempcells.setCellValue(key);
if (list != null && list.size() > 0) {
Row row = sheet.getRow(3);
if (row == null) {
row = sheet.createRow(2);
}
Cell tempcell = row.getCell(3);
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("ZWHZZ"); //这个地方就是第一个数据
System.out.println(list.toString());
tempcell.setCellValue(obj.toString());
row = sheet.getRow(3);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(3);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("ZWHZZNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(4);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(4);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("GLXSHY");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(4);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(4);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("GLXSHYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(5);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(5);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("QTHY");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(5);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(5);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("QTHYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(6);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(6);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("JOINSTUDYSTATENUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(6);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(6);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("STUDYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(7);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(7);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("INVESTIGATION");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(7);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(7);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("RDYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(8);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(8);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("INSPECTION");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(8);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(8);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("RSCNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(9);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(9);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("INVESTIGATE");
tempcell.setCellValue(obj.toString());
/*
if (row == null) {
row = sheet.createRow(9);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
row = sheet.getRow(9);
tempcell = row.getCell(5);
obj = list.get("RKCNUM");
tempcell.setCellValue(obj.toString());*/
row = sheet.getRow(10);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(10);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("DENUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(10);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(10);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("DERNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(11);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(11);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("TANUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(11);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(11);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("TARNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(12);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(12);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("QTNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(12);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(12);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("QTRNUM");
tempcell.setCellValue(obj.toString());
}
sheet.setForceFormulaRecalculation(true);
try {
OutputStream os = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename="
+ new String(URLEncoder.encode("省政协专委会组织委员开展活动情况季度统计表", "utf-8").getBytes("UTF-8"), "ISO-8859-1")
+ ".xls");
response.setContentType("application/msexcel");
excel.write(os);
os.close();
excel.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}