日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java导出类_java导出excel工具类

發布時間:2025/3/20 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java导出类_java导出excel工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java導出excel須要使用HSSFWorkbook這個類,須要導入poi-3.6-20091214.jar

工具類調用例如以下:

package com.qlwb.business.util;

import java.io.IOException;

import java.io.OutputStream;

import java.net.URLEncoder;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.ss.util.CellRangeAddress;

/**

* 導出報表的公共類,提供公共的導出報表的方法

* @Copyright: Copyright (c) 2015

* @Company:

* @author 鹿偉偉

*

*/

public class ExportExcel {

static boolean tempDirflag1 = true;

static boolean tempDirflag2 = true;

/**

* 依據傳入的集合數據生成報表的方法

*

* @param list

* 報表標題表頭及數據封裝的list,格式:list的第一條數據是標題。String類型;第二條數據是表頭列表,List類型;從第三條開始是數據列表

* ,List類型

* @param filename

* 報表excel的文件名稱,不須要擴展名

* @param response

* HttpServletResponse

* @return 1表示成功 0表示失敗

* @throws IOException

* @Create luweiwei 2015-08-18

* @change

*/

@SuppressWarnings("unchecked")

public static int createReport(List list, String filename,

HttpServletResponse response) throws IOException {

// 數據list至少包含標題和表頭

if (list.size() < 2) {

return 0;

}

// 組裝excel

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFSheet sheet = workbook.createSheet();

// 設置Excel標題字體和樣式

HSSFFont headFont = workbook.createFont();

headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

headFont.setFontHeightInPoints((short) 20);

HSSFCellStyle headCellStyle = workbook.createCellStyle();

headCellStyle.setFont(headFont);

// headCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);

// headCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

// headCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

// headCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

headCellStyle.setWrapText(true);

headCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// headCellStyle.setFillForegroundColor(GREEN_COLOR);

// 設置Excel表頭的字體和樣式

HSSFFont thfont = workbook.createFont();

thfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

HSSFCellStyle thCellStyle = workbook.createCellStyle();

thCellStyle.setFont(thfont);

thCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);

thCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

thCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

thCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

thCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

thCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

thCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

// 設置Excel內容的字體和樣式

HSSFFont font = workbook.createFont();

font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

HSSFCellStyle titleCellStyle = workbook.createCellStyle();

titleCellStyle.setFont(font);

titleCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);

titleCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

titleCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

titleCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);

// titleCellStyle.setFillForegroundColor(GREEN_COLOR);

// 單元格居左樣式

HSSFCellStyle cellLeft = null;

// 單元格居中樣式

HSSFCellStyle cellCenter = null;

// 單元格居右樣式

HSSFCellStyle cellRight = null;

// 單元格居左樣式

cellLeft = workbook.createCellStyle();

cellLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);

cellLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);

cellLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);

cellLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);

cellLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);

// 單元格居中樣式

cellCenter = workbook.createCellStyle();

cellCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);

cellCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);

cellCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);

cellCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);

cellCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// 單元格居右樣式

cellRight = workbook.createCellStyle();

cellRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);

cellRight.setBorderTop(HSSFCellStyle.BORDER_THIN);

cellRight.setBorderRight(HSSFCellStyle.BORDER_THIN);

cellRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);

cellRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

// 第一行

// 合并標題單元格

sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,

((List) list.get(1)).size() - 1));

HSSFRow row = sheet.createRow(0);

row.setHeightInPoints(40);

HSSFCell cell = null;

cell = row.createCell(0);

cell.setCellStyle(headCellStyle);

cell.setCellValue((String) (list.get(0)));

// 設置表頭單元格

List tmpList = null;

tmpList = (List) list.get(1);

int rownum = sheet.getLastRowNum();

row = sheet.createRow(rownum + 1);

for (int i = 0; i < tmpList.size(); i++) {

sheet.setColumnWidth(i, 6000);

cell = row.createCell(i);

cell.setCellStyle(thCellStyle);

cell.setCellValue(tmpList.get(i));

}

// 循環設置數據的單元格

for (int i = 2; i < list.size(); i++) {

tmpList = (List) list.get(i);

rownum = sheet.getLastRowNum();

row = sheet.createRow(rownum + 1);

for (int j = 0; j < tmpList.size(); j++) {

cell = row.createCell(j);

cell.setCellStyle(titleCellStyle);

cell.setCellValue(tmpList.get(j));

}

}

response.setHeader("Content-disposition", "attachment; filename="

+ URLEncoder.encode(filename, "utf-8") + ".xls");

response.setContentType("application/msexcel;charset=utf-8");

OutputStream out = null;

try {

out = response.getOutputStream();

out.flush();

workbook.write(out);

} finally {

try {

if(out!=null){

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return 1;

}

}

總結

以上是生活随笔為你收集整理的java导出类_java导出excel工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。