这篇文章主要为大家详细介绍了java导出数据库中Excel表格数据的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本篇文章基于java把数据库中的数据以Excel的方式导出,欢迎各位大神吐槽:
1、基于maven jar包引入如下:
net.sourceforge.jexcelapijxl2.6.12
2、首先创建数据库对应的实体类VO :UserVO(具体代码省略);
3、确定导出Excel内的title列,并放在数组里:String[] (具体代码省略);
4、编写导出Excel的方法:
传入参数:
Excel名称,Excel内的title列数组String[],数据集合List
package bp.util; import java.io.OutputStream; import java.util.List; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Field; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLinestyle; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExportExcel { public final static String exportExcel(String fileName, String[] Title, List listContent, HttpServletResponse response) { String result = "Excel文件导出成功!"; try { OutputStream os = response.getoutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1")); response.setContentType("application/msexcel"); WritableWorkbook workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet("Sheet1", 0); jxl.SheetSettings sheetset = sheet.getSettings(); sheetset.setProtected(false); WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD); WritableCellFormat wcf_center = new WritableCellFormat(BoldFont); wcf_center.setBorder(Border.ALL, BorderLinestyle.THIN); wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); wcf_center.setAlignment(Alignment.CENTRE); wcf_center.setWrap(true); for (int i = 0; i