java数据记录导出到Excel
2016-08-17 20:10
225 查看
在这里介绍的主要是用 Apache POI的方式来导出。首先下载到官方(https://www.apache.org/dyn/closer.lua/poi/dev/bin/poi-bin-3.15-beta2-20160702.tar.gz)下载jar包,所需要的jar包名称如下,下面来直接看代码:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.Region;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.Region;
</pre><pre name="code" class="java">public static void commonExportData(String title,Object[] obj, Vector v, HttpServletResponse response) { response.setContentType("application/vnd.ms-excel"); if ((title == null) || (title.equals(""))){ title = "合同导出"; } String sheetName = title; try { title = title + "_"+DateUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss"); title = new String(title.getBytes("gb2312"), "iso-8859-1"); response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xls"); response.setHeader("Pragma", "no-cache"); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet(); wb.setSheetName(0, sheetName.trim()); sheet1.setDefaultRowHeightInPoints(20.0F); sheet1.setDefaultColumnWidth((short)18); HSSFFooter footer = sheet1.getFooter(); footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages()); HSSFCellStyle style1 = wb.createCellStyle(); style1.setAlignment((short)2); HSSFFont font1 = wb.createFont(); font1.setFontHeightInPoints((short)13); font1.setBoldweight((short)700); style1.setFont(font1); HSSFCellStyle style2 = wb.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_LEFT); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = wb.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); /*HSSFCellStyle style2 = wb.createCellStyle(); style2.setAlignment((short)2); style2.setWrapText(true); style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);*/ //sheet1.addMergedRegion(new Region(0, (short)0, 0, (short)(obj.length - 2))); /*HSSFCellStyle style3 = wb.createCellStyle(); style3.setAlignment((short)2); HSSFFont font3 = wb.createFont(); font3.setFontHeightInPoints((short)20); font3.setBoldweight((short)700); style3.setFont(font3); HSSFRow row0 = sheet1.createRow(0); row0.setHeightInPoints(35.0F); HSSFCell cell0 = row0.createCell((short)0); cell0.setCellValue(obj[0].toString()); cell0.setCellStyle(style3);*/ HSSFRow row1 = sheet1.createRow(0); row1.setHeightInPoints(20.0F); for (int i = 0; i < obj.length; i++) { HSSFCell cell1 = row1.createCell((short)(i)); cell1.setCellValue(obj[i].toString()); cell1.setCellStyle(style1); } for (int j = 0; j < v.size(); j++) { HSSFRow row2 = sheet1.createRow((short)(j + 1)); Object[] o = (Object[])v.get(j); for (int k = 0; k < o.length; k++) { HSSFCell cell = row2.createCell((short)k); cell.setCellValue(o[k] == null ? "" : o[k].toString()); cell.setCellStyle(style2); } } wb.write(response.getOutputStream()); response.getOutputStream().flush(); response.getOutputStream().close(); } catch (Exception localException) { } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 使用Python生成Excel格式的图片
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序