//获取数据
//List<User> users = userService.selectUsers();
List<User> users =new ArrayList<User>();
User a=new User(1,"aabbbbbbbbbbbbbbbbbbbbbb","aaa");
User b=new User(2,"bb","bbb");
users.add(a);
users.add(b);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("获取excel测试表格");
HSSFRow row = null;
//第一行
//设置样式(标题样式)
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
/*style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);*/
//设置字体
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
font.setFontName("黑体");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font.setFontHeightInPoints((short) 25);
style.setFont(font);
//合并单元格
Region mergeRegion3638 = new Region(0, (short) 0, 0, (short) 2);
sheet.addMergedRegion(mergeRegion3638);
sheet.autoSizeColumn((short) 1);
row = sheet.createRow(0);//创建第一个单元格
row.setHeight((short) (26.25 * 20));
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(style);
cell.setCellValue("用户信息列表");
//第二行
//设置样式(表头)
HSSFCellStyle cellStyleHead = wb.createCellStyle();
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short) 12);
font1.setColor(HSSFColor.RED.index);
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font1.setFontName("黑体");
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
cellStyleHead.setFont(font1);
int rowIndex = 1;
int colIndex = 0;
row = sheet.createRow(rowIndex++);
cell = row.createCell((short) colIndex++);
String[] headTitles = { "第1列头部", "第2列头部", "第3列头部" };
for (int i = 0; i < headTitles.length; i++) {
cell = row.createCell((short) i);
cell.setCellStyle(cellStyleHead);
cell.setCellValue(new HSSFRichTextString(headTitles[i]));
}
//第三行往后
//设置样式(文本)
HSSFCellStyle cellStyleContent = wb.createCellStyle();
HSSFFont font2 = wb.createFont();
font2.setFontHeightInPoints((short) 10);
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
cellStyleContent.setFont(font2);
for (int i = 0; i < users.size(); i++) {
row = sheet.createRow(i + 2);
User user = users.get(i);
row.createCell((short) 0).setCellValue(user.getId());
row.createCell((short) 1).setCellValue(user.getUsername());
row.createCell((short) 2).setCellValue(user.getPassword());
}
sheet.setDefaultRowHeight((short) (20 * 40));
//列宽自适应
for (int i = 0; i <= 13; i++) {
sheet.autoSizeColumn((short) i);
}
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=12345.xls");//默认Excel名称
response.setCharacterEncoding("utf-8");
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
=================================
package com.poi.testpoi.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.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.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping("/system1")
@Controller
public class exportController {
@RequestMapping(value = "/export" ,method=RequestMethod.GET)
@ResponseBody
public void export(HttpServletResponse response) throws IOException {
String fileName = "e://"+System.currentTimeMillis()+".xls";
String[] header = new String[]{"经销商", "年卡号", "激活码"};
List<String> listAgent = new ArrayList<String>();
listAgent.add("steven");
listAgent.add("peter");
listAgent.add("vactor");
// 导出到多个sheet中--------------------------------------------------------------------------------开始
// 创建一个EXCEL
HSSFWorkbook wb = new HSSFWorkbook();
// 循环经销商,每个经销商一个sheet
for (int i = 0; i < listAgent.size(); i++)
{
// 第 i 个sheet,以经销商命名
HSSFSheet sheet = wb.createSheet(listAgent.get(i).toString());
//第一行前三个单元格合并(参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号)
Region region1 = new Region(0, (short) 0,(short)0,(short)2);
sheet.addMergedRegion(region1);
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell1 = row1.createCell((short) 0);
HSSFCell cell2 = row1.createCell((short)1);
HSSFCell cell3 = row1.createCell((short)2);
cell1.setCellValue("第一行设置标题");
//单元格 设置样式 ----
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
cellStyle.setFillForegroundColor((short) 13);// 设置背景色
//设置字体
HSSFFont font = wb.createFont();
font.setFontName("仿宋_GB2312");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);//字体设置应用到设置的格式上
//使 设置的样式生效(设置的格式应用到单元格上)
cell1.setCellStyle(cellStyle);
cell2.setCellStyle(cellStyle);
/** ^
*
*
*/
cell3.setCellStyle(cellStyle);
// 第i个sheet第二行为列名
HSSFRow rowFirst = sheet.createRow(1);
// 写标题
for (int j = 0; j < header.length; j++)
{
// 获取第一行的每一个单元格
HSSFCell cell = rowFirst.createCell((short) j);
// 往单元格里面写入值
cell.setCellValue(header[j]);
}
// 循环该经销商下的卡(为每一个sheet准备数据)
// List<StudyCard> lst = study_cardService.list(paramMap, null);
List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
// 循环添加测试数据
for (int j = 0; j < 10; j++)
{
Map<String, Object> data = new HashMap<String, Object>(3);
data.put("agent", listAgent.get(i).toString() + j);
data.put("card_no", "card" + j);
data.put("password", "password" + j);
lst.add(data);
}
System.out.println(lst);//三个list,list中每一行是个map[{card_no=card0, agent=steven0, password=password0}, {card_no=card1, agent=steven1, password=password1}]
for (int j = 0; j < lst.size(); j++)
{
Map<?, ?> map = (Map<?, ?>) lst.get(j);
// 创建数据行,从第三行开始
HSSFRow row = sheet.createRow(j + 2);
// 设置对应单元格的值
row.createCell((short)0).setCellValue(map.get("agent").toString());
row.createCell((short)1).setCellValue(map.get("card_no").toString());
row.createCell((short)2).setCellValue(map.get("password").toString());
}
}
// 写出文件(path为文件路径含文件名)
OutputStream os = null;
try
{
os = new FileOutputStream(new File(fileName));
wb.write(os);
System.out.println("文件导出成功,请前往查看.....\n" + fileName);
}
catch (Exception e)
{
System.out.println("我擦,导出文件居然出错了.....\n" + e.getMessage());
}
finally
{
os.flush();
os.close();
}
// 导出到多个sheet中---------------------------------------------------------------------------------结束
}
}