jxl paging excel

The following code is for reference only:
//Generate an excel file
WritableWorkbook wwb = null;
try {
//First use the factory method of the Workbook class to create a writable Workbook object
wwb = Workbook.createWorkbook(new File( fileName));
} catch (IOException e) {
log.error(e); }
long totle = service.getTotle(searchParameters);//Business logic method, get the total number, easy to divide multiple worksheets in excel
//Press 65536 Data paging
float res=Float.parseFloat(String.valueOf(totle));
float mus=65536;
float avg=res/mus;
Map cols = (Map) request.getSession().getAttribute("columnsMap"); / /Business logic method
for (int i = 0; i < avg+1; i++) {
searchParameters.setEvent_id(String.valueOf(i*mus)); //Pagination query condition
searchParameters.setTotalLimit(String.valueOf((i+ 1)*mus));//Pagination query conditions
List result = service.getEvents(searchParameters); //Paging query method

if(wwb!=null){
//Create a writable worksheet
//The createSheet method of Workbook has two parameters, the first is the worksheet The name of the sheet and the second is the position of the sheet in the workbook
WritableSheet ws = wwb.createSheet("list"+(i+1), i);
String colss = ((String) cols.get("cols") ).substring("selected"
.length() + 1);
String[] columns = colss.split(","); //Business logic method (add title)
String[] colNames = ((String) cols.get ("colNames")).split(",");
for (int j = 0; j < columns.length; j++) {
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,10, WritableFont.NO_BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfFC = new jxl. write.WritableCellFormat(wfc);
wcfFC.setBackground(Colour.GRAY_25);
Label label = new Label(j, 0,colNames[j],wcfFC);
ws.setColumnView(j, 20); //Set the column width
ws.addCell(label); // Add title
}
String str="";
//Start adding cells below
for(int m=0;m<result.size();m++){
for(int j=0;j<columns.length;j++){
Map map = (Map) result.get(m);
//Add (business data) to the table
for (Object key : map.keySet()) {
Object val = map.get(key);
str=String.valueOf (val);
//It should be noted here that in Excel, the first parameter represents the column, and the second represents the row
Label labelC = new Label(j, m+1, str);
//The cell that will be generated Add to the worksheet
ws.addCell(labelC);
}

}
}
}

}
//After the Excel operation is completed, close all operation resources
try {
//Write to the file from memory
wwb.write();
//Close resources and release memory
wwb.close();
} catch (IOException e) {
log.error(e);
} catch (WriteException e) {
log.error(e);
}

//Download the generated file
File file = new File(fileName);
if(!file.exists()) throw new Exception("File does not exist!");
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
OutputStream outputStream = response.getOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("事件列表.xls", "UTF-8"));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bufferedInputStream.read(buffer, 0, 8192)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead);
}
bufferedOutputStream.flush();
fileInputStream.close();
bufferedInputStream.close();
outputStream.close();
bufferedOutputStream.close();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326975614&siteId=291194637
jxl