1. 程式人生 > >java導出excel表格方法

java導出excel表格方法

name tps 由於 inf catch 做網站 com 產生 add

深圳網站建設公司,一般會使用asp.net,java,php做網站底層開發,由於網站使用過程中,會產生大量數據,這些數據包含留言信息,或者新聞信息,由於我們今天是做java網站建設,來開發網站,所以我們就來講一下java導出excel表格的方法。

1、準備工作,下載且導入poi.jar
由於我們今天講的導出excel表格的方法是使用第三方jar來導出的,所以需要先導入第三方poi.jar包。

2、制作導出excel的方法
public static HSSFWorkbook getHSSFWorkbook(String sheetName,
String[] title, ArrayList<ArrayList<TableInfo>> list,

HSSFWorkbook wb) {

// 第一步,創建一個HSSFWorkbook,對應一個Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}

// 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);

// 第三步,在sheet中添加表頭第0行,註意老版本poi對Excel的行數列數有限制
HSSFRow row = sheet.createRow(0);

// 第四步,創建單元格,並設置值表頭 設置表頭居中
HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式

// 聲明列對象
HSSFCell cell = null;

// 創建標題
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}

int i = 0;

    //判斷是否有內容

if (list != null) {
// 創建內容
for (ArrayList<TableInfo> mlist : list) {

//得到行號
row = sheet.createRow(i + 1);
//循環所有信息
for (TableInfo model : mlist) {

if (model.getName().equals("subject")) {
//向行號插入信息
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++;
}
}

// 讓列寬隨著導出的列長自動適應
for (int colNum = 0; colNum < 9; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
// 當前行未被使用過
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();
    }
    }
    通過上面兩個方法我們就可以制作java網站建設導出excel表格的功能。
    深圳網站建設https://www.sz886.com

java導出excel表格方法