POI 동적 내보내기 EXCEL
- /**
- * POI3.0.2 EXCEL
- *
- * @author
- * @version v1.0
- * @param <T>
- *
- */
- public class ExportExcel<T> {
-
- public void exportExcel(Collection<T> dataset, OutputStream out) {
- exportExcel(" EXCEL ", null, dataset, out, "yyyy-MM-dd");
- }
-
- public void exportExcel(String[] headers, Collection<T> dataset,
- OutputStream out) {
- exportExcel(" EXCEL ", headers, dataset, out, "yyyy-MM-dd");
- }
-
- public void exportExcel(String[] headers, Collection<T> dataset,
- OutputStream out, String pattern) {
- exportExcel(" EXCEL ", headers, dataset, out, pattern);
- }
-
- /**
- * , JAVA , JAVA EXCEL IO
- *
- * @param title
- *
- * @param headers
- *
- * @param dataset
- * , javabean 。
- * javabean String,Date,byte[]( )
- * @param out
- * , EXCEL
- * @param pattern
- * , 。 "yyy-MM-dd"
- */
- @SuppressWarnings({ "unchecked", "deprecation" })
- public void exportExcel(String title, String[] headers,
- Collection<T> dataset, OutputStream out, String pattern) {
- //
- HSSFWorkbook workbook = new HSSFWorkbook();
- //
- HSSFSheet sheet = workbook.createSheet(title);
- // 15
- sheet.setDefaultColumnWidth((short) 15);
- //
- HSSFCellStyle style = workbook.createCellStyle();
- //
- style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
- style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
- style.setBorderRight(HSSFCellStyle.BORDER_THIN);
- style.setBorderTop(HSSFCellStyle.BORDER_THIN);
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- //
- HSSFFont font = workbook.createFont();
- font.setColor(HSSFColor.VIOLET.index);
- font.setFontHeightInPoints((short) 12);
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- //
- style.setFont(font);
- //
- HSSFCellStyle style2 = workbook.createCellStyle();
- style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
- style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
- style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
- style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
- style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- //
- HSSFFont font2 = workbook.createFont();
- font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
- //
- style2.setFont(font2);
-
- //
- HSSFRow row = sheet.createRow(0);
- for (short i = 0; i < headers.length; i++) {
- HSSFCell cell = row.createCell(i);
- cell.setCellStyle(style);
- HSSFRichTextString text = new HSSFRichTextString(headers[i]);
- cell.setCellValue(text);
- }
-
- // ,
- Iterator<T> it = dataset.iterator();
- int index = 0;
- while (it.hasNext()) {
- index++;
- row = sheet.createRow(index);
- T t = (T) it.next();
- // , javabean , getXxx()
- Field[] fields = t.getClass().getDeclaredFields();
- for (short i = 0; i < fields.length; i++) {
- HSSFCell cell = row.createCell(i);
- // cell.setCellStyle(style2);
- Field field = fields[i];
- String fieldName = field.getName();
- String getMethodName = "get"
- + fieldName.substring(0, 1).toUpperCase()
- + fieldName.substring(1);
- try {
- Class tCls = t.getClass();
- Method getMethod = tCls.getMethod(getMethodName,
- new Class[] {});
- Object value = getMethod.invoke(t, new Object[] {});
- //
- String textValue = null;
-
- if (value instanceof Date) {
- Date date = (Date) value;
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- textValue = sdf.format(date);
- } else {
- //
- textValue = value.toString();
- }
- // textValue
- if (textValue != null) {
- Pattern p = Pattern.compile("^//d+(//.//d+)?{1}quot");
- Matcher matcher = p.matcher(textValue);
- if (matcher.matches()) {
- // double
- cell.setCellValue(Double.parseDouble(textValue));
- } else {
- HSSFRichTextString richString = new HSSFRichTextString(
- textValue);
- HSSFFont font3 = workbook.createFont();
- font3.setColor(HSSFColor.BLUE.index);
- richString.applyFont(font3);
- cell.setCellValue(richString);
- }
- }
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- //
- }
- }
-
- }
- try {
- workbook.write(out);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- public static void main(String[] args) throws IOException {
- ExportExcel<ReportBillsModel> ex = new ExportExcel<ReportBillsModel>();
- String[] headers = { " ", " ", " ", " ", " ",
- " ", " " };
- List<ReportBillsModel> ls = new ArrayList<ReportBillsModel>();
- ReportBillsModel rm = new ReportBillsModel();
- rm.setAgentName("test");
- rm.setBankName("bank");
- rm.setCheckTime("1992-21-21");
- rm.setCreditorName("aa");
- rm.setDeductionAmount(21);
- rm.setDeductionTime("2012-12-12");
- rm.setEstimationCode("aadf");
- rm.setResidenceAddr("aaaaaaaaaaaaa");
-
- ls.add(rm);
- try {
- OutputStream out = new FileOutputStream("E://a.xls");
- ex.exportExcel(headers, ls, out);
- out.close();
-
- System.out.println("excel !");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
웹 응용 프로그램의 경우 다음과 같은 다운로드 기능을 제공합니다.
- HttpServletResponse response = getResponse();
- response.setContentType("application/octet-stream");
- String fileName = URLEncoder.encode("bills.xls",
- "UTF-8");
- String contentDisposition = "attachment; filename=" + fileName;
- response.setHeader("Content-Disposition", contentDisposition);
- ExportExcel<ReportBillsModel> ex = new ExportExcel<ReportBillsModel>();
- OutputStream out = response.getOutputStream();
- ex.exportExcel(headers, rbmList, out);
- out.close();
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Fortinet FortiWeb Web Application Firewall Policy BypassFrom: Geffrey Velasquez Date: Wed, 2 May 2012 20:33:23 -0500...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.