java在向Excel表中固定的sheet表插入数值时如何获取sheet名和行号
1个回答
展开全部
WritableWorkbook workBook = null;
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/vnd.ms-excel");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String excelName = "yuyueliebiao" + format.format(new Date());
response.setHeader("Content-Disposition", "attachment; filename=\"" + excelName + ".xls\"");
response.setHeader("Cache-Control", "private");
response.setHeader("Pragma", "");
workBook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet sheet = workBook.createSheet("sheet", 0);
Label label;
for (int i = 0; i < infs.size(); i++) {
HashMap inf = (HashMap) infs.get(i);
int j = 0;
label = new Label(j++, i, (String) inf.get("djlx"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("zch"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("qylx"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("qymc"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("yyrq"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("sqr"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("djjg"));
sheet.addCell(label);
}
workBook.write();
response.getOutputStream().flush();
response.getOutputStream().close();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/vnd.ms-excel");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String excelName = "yuyueliebiao" + format.format(new Date());
response.setHeader("Content-Disposition", "attachment; filename=\"" + excelName + ".xls\"");
response.setHeader("Cache-Control", "private");
response.setHeader("Pragma", "");
workBook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet sheet = workBook.createSheet("sheet", 0);
Label label;
for (int i = 0; i < infs.size(); i++) {
HashMap inf = (HashMap) infs.get(i);
int j = 0;
label = new Label(j++, i, (String) inf.get("djlx"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("zch"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("qylx"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("qymc"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("yyrq"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("sqr"));
sheet.addCell(label);
label = new Label(j++, i, (String) inf.get("djjg"));
sheet.addCell(label);
}
workBook.write();
response.getOutputStream().flush();
response.getOutputStream().close();
追问
要是Excel表的格式也固定了呢,就是表已存在不用新建
追答
你按照已有的格式往里面写数据就行了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询