码迷,mamicode.com
首页 > 编程语言 > 详细

Java Excel导出方法(POI或jxl)

时间:2018-12-25 18:05:16      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:utf-8   pass   设置   excel   tab   name   nal   random   sed   

1.使用POI组件

/**
* 导出excel
* @param wb
* @param sheetname
* @param head
* @param List<Map<String,Objecg>> dataList
*/
private static void createSheetForBaseData(XSSFWorkbook wb,
String sheetname, Map<String,String> head, List<Map<String,Object>> dataList) {
XSSFSheet sheet = wb.createSheet(sheetname);
XSSFRow headRow = sheet.createRow(0);
List<String> key=new ArrayList<String>();
List<String> value=new ArrayList<String>();
for (Map.Entry<String, String> entry:head.entrySet()) {
key.add(entry.getKey());
value.add(entry.getValue());
}
for (int i = 0; i < value.size() ;i++) {
XSSFCell cell = headRow.createCell(i);
cell.setCellValue(value.get(i).toString());
}
try {
for (int j = 0; j < dataList.size(); j++) {
XSSFRow row = sheet.createRow(j + 1);
for (int i = 0; i < key.size(); i++) {
Object val = dataList.get(j).get(key.get(i));
val = val == null ? "-" : val;
XSSFCell cell = row.createCell(i);
// cell.setCellStyle(style2);
cell.setCellValue(val.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}

sheet.protectSheet("123456");//设置工作表保护密码
}

 

2.jxl组件(加文字水印)

private static void createSheetForBaseDataWaterMark(HttpServletResponse response,
String sheetname, Map<String,String> head, List<Map<String,Object>> dataList, Map<String, String> mapWaterMark) {
WritableWorkbook workbook = null;
OutputStream os =null;
FileInputStream fis = null;

try {
os = response.getOutputStream();
// 取得输出流
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(sheetname.getBytes("gb2312"), "iso8859-1")+".xls");//fileName为下载时用户看到的文件名利用jxl 将数据从后台导出为excel
response.setHeader("Content-Type", "application/msexcel");

workbook = Workbook.createWorkbook(os);
//创建一个sheet
WritableSheet sheet = workbook.createSheet(sheetname, 0);
List<String> key=new ArrayList<String>();
List<String> value=new ArrayList<String>();
for (Map.Entry<String, String> entry:head.entrySet()) {
key.add(entry.getKey());
value.add(entry.getValue());
}
//第一行
for (int i = 0; i < value.size() ;i++) {
Label label = new Label(i, 0, value.get(i).toString());
sheet.addCell(label);
}

for (int j = 0; j < dataList.size(); j++) {
for (int i = 0; i < key.size(); i++) {
Object val = dataList.get(j).get(key.get(i));
val = val == null ? "-" : val;
Label label = new Label(i, j+1, val.toString());
sheet.addCell(label);
}
}

//水印图片文件
File fileImg = createWaterMark(mapWaterMark);
byte imageData[] = new byte[(int) fileImg.length()];
fis = new FileInputStream(fileImg);
fis.read(imageData);
sheet.setWaterMarkImage(imageData, 480, 1020);
//设置密码保护
sheet.setProtected(true);
SheetSettings settings = sheet.getSettings();
settings.setPassword(UUID.randomUUID().toString());
workbook.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
workbook.close();
os.close();
fis.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

 

/**
* 生成水印图片
*
* @param mapWaterMark
* @return
* @throws IOException
*/
public static File createWaterMark(Map<String, String> mapWaterMark) throws IOException {

int width = 480; // 水印图片的宽度
int height = 1020; // 水印图片的高度 因为设置其他的高度会有黑线,所以拉高高度
File watermarkFileName = new File(System.getProperty("user.dir") + "/" + System.currentTimeMillis() + ".bmp");
// 获取bufferedImage对象
BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
// 处理背景色,设置为 白色
int minx = bi.getMinX();
int miny = bi.getMinY();
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
bi.setRGB(i, j, 0xffffff);
}
}

// 获取Graphics2d对象
Graphics2D g2d = bi.createGraphics();
// 设置字体颜色为灰色
g2d.setColor(Color.LIGHT_GRAY);
// 设置图片的属性
g2d.setStroke(new BasicStroke(1));
// 设置字体
g2d.setFont(new java.awt.Font("Serif", Font.ITALIC, 20));
// 设置字体倾斜度
g2d.rotate(Math.toRadians(-10));

// 写入水印文字 原定高度过小,所以累计写水印,增加高度
Object[] arrayWaterMark = mapWaterMark.keySet().toArray();
for (int i = 0; i < mapWaterMark.size(); i++) {
//水印距离顶部高度120
g2d.drawString(arrayWaterMark[i] + ":" + mapWaterMark.get(arrayWaterMark[i]), 0, 120 + 40 * (i+1));
}
// 设置透明度
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 释放对象
g2d.dispose();
// 通过bmp写入文件
BMPEncoder.write(bi, watermarkFileName);

return watermarkFileName;
}

 

Java Excel导出方法(POI或jxl)

标签:utf-8   pass   设置   excel   tab   name   nal   random   sed   

原文地址:https://www.cnblogs.com/pg3587/p/10175241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!