java網(wǎng)站建設(shè)導(dǎo)出excel表格方法
2018/8/1 11:11:50 獨(dú)占網(wǎng)絡(luò) 新聞中心
深圳網(wǎng)站建設(shè)公司,一般會(huì)使用asp.net,java,php做網(wǎng)站底層開(kāi)發(fā),由于網(wǎng)站使用過(guò)程中,會(huì)產(chǎn)生大量數(shù)據(jù),這些數(shù)據(jù)包含留言信息,或者新聞信息,由于我們今天是做java網(wǎng)站建設(shè),來(lái)開(kāi)發(fā)網(wǎng)站,所以我們就來(lái)講一下java導(dǎo)出excel表格的方法。
1、準(zhǔn)備工作,下載且導(dǎo)入poi.jar
由于我們今天講的導(dǎo)出excel表格的方法是使用第三方j(luò)ar來(lái)導(dǎo)出的,所以需要先導(dǎo)入第三方poi.jar包。
2、制作導(dǎo)出excel的方法
public static HSSFWorkbook getHSSFWorkbook(String sheetName,
String[] title, ArrayList<ArrayList<TableInfo>> list,
HSSFWorkbook wb) {
// 第一步,創(chuàng)建一個(gè)HSSFWorkbook,對(duì)應(yīng)一個(gè)Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表頭第0行,注意老版本poi對(duì)Excel的行數(shù)列數(shù)有限制
HSSFRow row = sheet.createRow(0);
// 第四步,創(chuàng)建單元格,并設(shè)置值表頭 設(shè)置表頭居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創(chuàng)建一個(gè)居中格式
// 聲明列對(duì)象
HSSFCell cell = null;
// 創(chuàng)建標(biāo)題
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
int i = 0;
//判斷是否有內(nèi)容
if (list != null) {
// 創(chuàng)建內(nèi)容
for (ArrayList<TableInfo> mlist : list) {
//得到行號(hào)
row = sheet.createRow(i + 1);
//循環(huán)所有信息
for (TableInfo model : mlist) {
if (model.getName().equals("subject")) {
//向行號(hào)插入信息
row.createCell(0).setCellValue(model.getValue());
} else if (model.getName().equals("name")) {
row.createCell(1).setCellValue(model.getValue());
} else if (model.getName().equals("sex")) {
row.createCell(2).setCellValue(model.getValue());
} else if (model.getName().equals("birdate")) {
row.createCell(3).setCellValue(model.getValue());
} else if (model.getName().equals("company")) {
row.createCell(4).setCellValue(model.getValue());
} else if (model.getName().equals("position")) {
row.createCell(5).setCellValue(model.getValue());
} else if (model.getName().equals("tel")) {
row.createCell(6).setCellValue(model.getValue());
}else if (model.getName().equals("email")) {
row.createCell(7).setCellValue(model.getValue());
} else if (model.getName().equals("address")) {
row.createCell(8).setCellValue(model.getValue());
}
}
i++;
}
}
// 讓列寬隨著導(dǎo)出的列長(zhǎng)自動(dòng)適應(yīng)
for (int colNum = 0; colNum < 9; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
// 當(dāng)前行未被使用過(guò)
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue()
.getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if (colNum == 0) {
sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
} else {
sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
}
}
return wb;
}
3、第三步下載信息
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
通過(guò)上面兩個(gè)方法我們就可以制作java網(wǎng)站建設(shè)導(dǎo)出excel表格的功能。
java網(wǎng)站建設(shè),asp.net網(wǎng)站建設(shè),可以優(yōu)先選擇獨(dú)占網(wǎng)絡(luò),專業(yè),技術(shù)開(kāi)發(fā)能力強(qiáng)。