http://poi.apache.org/download.html这是包的下载网站
---------------------------------------------------------------------------------------------------------
//导出数据到EXCEL表格
public static void ExportLgExcel(HttpServletRequest request, HttpServletResponse resp) throws Exception {
//读取指定目录下面的excel导出模板
String excelDir = request.getServletContext().getRealPath("/upload/ExcelStencil");
Workbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xlsx")));
} catch (Exception e) {
wb = new HSSFWorkbook(new FileInputStream(new File(excelDir + "/lightList.xls")));
}
// 创建字体,设置其为红色、粗体:
Font font = wb.createFont();
font.setColor(Font.COLOR_RED);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
//创建格式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
CellStyle cellStyleBorder = wb.createCellStyle();
cellStyleBorder.setBorderBottom(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderLeft(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderRight(CellStyle.BORDER_THIN);
cellStyleBorder.setBorderTop(CellStyle.BORDER_THIN);
//这个是从数据库里面查询出来的数据,存放到列表中
//Class log:数据库查询出数据的一个对象
List<Class log> result = new ArrayList<Class log>();
//获得第一个工作区
Sheet sheet = wb.getSheetAt(0);
if (!CommUtils.isNull(result)) {
//循环遍历查询出来的结果对象,写入到excel表格里面去
Integer listSize = result.size();
for (int i = 0; i < listSize; i++) {
Class log = result.get(i);
//因为第一行存放的是列标题,所以从第二行开始写入
//从第二行开始写入
Row row = sheet.createRow(i + 1);
//往表格里面填充数据
Integer cellLeng = log.getClass().getDeclaredFields().length;
for (int cellIndex = 0; cellIndex < cellLeng; cellIndex++) {
//设置excel表格的样式
Cell cell = row.createCell(cellIndex);
if (cellIndex < cellLeng - 1) {
cell.setCellStyle(cellStyleBorder);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
//将第一个数据保存到第一个单元格,依次类推
if (cellIndex == 0) {
String one = log.getXXXXX();
cell.setCellValue(one);
} else if (cellIndex == 1) {
String two = log.getXXXX();
cell.setCellValue(two);
} else if (cellIndex == 2) {
Integer three = log.getXXXXX();
cell.setCellValue(three.toString());
} else if (cellIndex > 2) {
break;
}
}
}
}
//生成导出文件日期
Calendar c = Calendar.getInstance();
String time = c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DATE) + "_" + c.get(Calendar.HOUR) + "_" + c.get(Calendar.MINUTE) + "_" + c.get(Calendar.SECOND);
//生成文件名(以下涉及到文件的输入和输出流)
File excelFile = new File("XXXXX_" + time + ".xls");
FileOutputStream fos = new FileOutputStream(excelFile);
wb.write(fos);
if (!CommUtils.isNull(fos)) {
fos.close();
}
// 读到流中
InputStream inStream = new FileInputStream(excelFile);
// 设置输出的格式
resp.reset();
resp.setContentType("application/x-download; charset=utf-8");
resp.addHeader("Content-Disposition", "attachment; filename=\"" + excelFile + "\"");
// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
try {
OutputStream out = resp.getOutputStream();
while (inStream.read(b) > 0) {
out.write(b);
}
inStream.close();
out.close();
excelFile.delete();
} catch (IOException ex) {
}
}