作者:壹舊雲A | 来源:互联网 | 2024-10-20 14:11
网上代码看了一堆感觉有点糊涂在这里请教高手如果利用jxl实现希望给个清晰明了的源代码谢谢啊不利用javabean的调用直接在jsp代码中实现客户端用户给该excel文件命名
网上代码看了一堆 感觉有点糊涂
在这里请教高手 如果利用jxl实现 希望给个清晰明了的源代码 谢谢啊
不利用javabean的调用 直接在jsp代码中实现 客户端用户给该excel文件命名
10 个解决方案
...不可能在客户端实现的,在服务端实现,然后传达室到客户端,
因为到了客户端, 已经不关 java 的事了
JXL的找不到了,我这里有POI的
要不先将就看一下
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(new File(request.getRealPath("")+"/Template/Model.xls")));
workbook=new HSSFWorkbook(fs);
List vwPrintList=(List)request.getAttribute("vwPrintList");
HSSFSheet sheet = workbook.getSheet("belongingsPrint");
// 0.4相当于1厘米,顺序为左右上下页边距
sheet.setMargin((short)0,1.2);
sheet.setMargin((short)1,0.4);
sheet.setMargin((short)2,0.8);
sheet.setMargin((short)3,0.8);
//true:横向 false:纵向
sheet.getPrintSetup().setLandscape(false);
Iterator vwPrintIt=vwPrintList.listIterator();
response.setContentType(getContentType());
HSSFFont fOnt=workbook.createFont();
font.setFontHeightInPoints((short)10);
//设置单元格格式
HSSFCellStyle cellStyle = workbook.createCellStyle();
// cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置自动换行
cellStyle.setFont(font);
cellStyle.setWrapText(true);
HSSFCell cell = null;
HSSFRow sheetRow = sheet.createRow(0);
HSSFFooter footer=sheet.getFooter();
int totalPage=0;
int lsize=vwPrintList.size();
if(lsize%27==0)
{
totalPage=lsize/27;
}else
{
totalPage=lsize/27+1;
}
String[] title=new String[]{"年月日","登录号","书名","作者","出版社","出版日期","页卷数","来源","册数","价格","索取号","备注"};
for(int i=0;i sheet.setColumnWidth((short)(0),(short)(12*150) ); //日期
sheet.setColumnWidth((short)(1),(short)(13*150) ); //登陆号
sheet.setColumnWidth((short)(2),(short)(62*150) ); //书名
sheet.setColumnWidth((short)(3),(short)(36*150) ); //作者
sheet.setColumnWidth((short)(4),(short)(40*150) ); //出版社
sheet.setColumnWidth((short)(5),(short)(13*150) ); //出版日期
sheet.setColumnWidth((short)(6),(short)(18*150) ); //页卷数
sheet.setColumnWidth((short)(7),(short)(7*150) ); //来源
sheet.setColumnWidth((short)(8),(short)(7*150) ); //册数
sheet.setColumnWidth((short)(9),(short)(10*150) ); //价格
sheet.setColumnWidth((short)(10),(short)(28*150) ); //索书号
sheet.setColumnWidth((short)(11),(short)(8*150) ); //备注
if(i%27==0)
{
sheetRow=sheet.createRow((short)i);
for(short j=0;j<12;j++){
sheetRow.setHeight((short)(480));
cell=sheetRow.createCell((j));
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(title[j]);
}
}else{
sheetRow = sheet.createRow((short)(i));
sheetRow.setHeight((short)(480));
VwPrint vwPrint=(VwPrint)vwPrintIt.next();
cell=sheetRow.createCell((short)0);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("");
cell=sheetRow.createCell((short)1);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getEntryNo());
cell=sheetRow.createCell((short)2);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getBookname());
cell=sheetRow.createCell((short)3);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getAuthor().trim(+" "+vwPrint.getOtherAuthor());
cell=sheetRow.createCell((short)4);
cell.setCellStyle(cellStyle);;
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getPublishAddress().trim()+":"+vwPrint.getPublisher());
cell=sheetRow.createCell((short)5);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getPublishDate());
cell=sheetRow.createCell((short)6);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getPageNum().trim()+vwPrint.getBookSize());
cell=sheetRow.createCell((short)7);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("");
cell=sheetRow.createCell((short)8);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getCopyNum());
cell=sheetRow.createCell((short)9);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getPrice());
cell=sheetRow.createCell((short)10);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vwPrint.getIndexNo());
cell=sheetRow.createCell((short)11);
cell.setCellStyle(cellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("");
}
}
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
直接在jsp代码中实现
建议最好还是用JavaBean与JSP分开,代码多了,JSP页面就会显得很臃肿