java web exports data as code snippets in Excel format files
- 2020-06-01 09:48:45
- OfStack
For your reference, the example of this article shares the specific code of java web to export data to Excel file. The specific content is as follows
1. jsp code
< input type="button" class="btn btn-info "onclick="getVerExcel();" value=" export as Excel file "/ >
2. js code
function getVerExcel() {
window.location.href = '/pms/jsp/version/getPrdVerListExcel?page='
+ $("#getPage").html() + '&key=' + $("#select").val();
}
3. java code
/**
*
* Purpose : Export the product version list as Excel file
* @param req
* request
* @param resp
* The reply
* @param page
* The current number of pages
* @param key
* Query conditions
* @return
*/
@RequestMapping("getPrdVerListExcel")
public void getExcel(HttpServletRequest req, HttpServletResponse resp, Integer page, String key) {
// Setup file mime type
resp.setContentType("application/vnd.ms-excel");
// Get all the data
List<Version> verList = prdVersionSer.getAllPrdVersion(key);
// If there is no data, the user is prompted
if (verList.size() == 0) {
req.setAttribute("getFileMsg", " No qualified information! ");
req.setAttribute("select", key);
try {
req.getRequestDispatcher("/jsp/version/ver_list.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// Stores the encoded file name
String name = "name";
// Name of storage file
String n = "";
if (key != "") {
n = verList.get(0).getPrdName() + " Version list of ";
} else {
n = " Product version list ";
}
try {
name = URLEncoder.encode(n, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
resp.setHeader("content-disposition",
"attachment;filename=" + name + ".xls;filename*=utf-8''" + name + ".xls");
System.out.println("key:" + key);
// from session Remove the saveExcelMsg attribute
req.getSession().removeAttribute("saveExcelMsg");
// define 1 An output stream
ServletOutputStream sos = null;
// create 1 A workbook
HSSFWorkbook wb = new HSSFWorkbook();
// create 1 A worksheet
HSSFSheet sheet = null;
if (key != "") {
sheet = wb.createSheet(verList.get(0).getPrdName() + " Version information ");
} else {
sheet = wb.createSheet(" Product version information ");
}
// Returns a data format object
// Gets the number of the corresponding date format from the format object, and if the format does not exist, the method generates a new number for it
HSSFDataFormat format = wb.createDataFormat();
short dfNum = format.getFormat("yyyy-mm-dd");
// Creating a style object
CellStyle style = wb.createCellStyle();
// Format the data
style.setDataFormat(dfNum);
// Create the first 1 Row (table header)
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
if (key != "") {
cell.setCellValue(verList.get(0).getPrdName() + " Product version list ");
} else {
cell.setCellValue(" Product version list ");
}
// Create the first 2 Line (header)
row = sheet.createRow(1);
cell = row.createCell(0, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(" The serial number ");
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(" The product name ");
cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(" The version number ");
cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(" The release date ");
cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(" Version type ");
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(" Version described ");
int num = 1;
// Traverse the output verList , and store it Excel In the
for (int i = 0; i < verList.size(); i++) {
row = sheet.createRow(i + 2);
// Write the serial number
cell = row.createCell(0, HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(num);
num++;
// Write the product name
cell = row.createCell(1, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getPrdName());
// Write version number
cell = row.createCell(2, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerNo());
// Write the release date ( Date format does the processing )
cell = row.createCell(3, HSSFCell.CELL_TYPE_STRING);
// Apply the style to the cell
cell.setCellStyle(style);
cell.setCellValue(verList.get(i).getVerDate());
// Write version type
cell = row.createCell(4, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerType());
// Write version description
cell = row.createCell(5, HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(verList.get(i).getVerDesc());
}
try {
// Save to a file
sos = resp.getOutputStream();
wb.write(sos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sos != null) {
try {
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}