apache poi excel word 加密,不用借助其他jxcell.jar包
代码可以进一步优化,只是个例子:
点击打开链接
public class TestExcel {public static void main(String[] args){try {SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(10000);Map<String, CellStyle> styles = createStyles(xssfWorkbook);SXSSFSheet sheet = createWorksheet(xssfWorkbook, "test");SXSSFRow row = sheet.createRow(0);createCell(row, 0, styles.get(CellStyleKey.CELL_NORMAL), Cell.CELL_TYPE_BLANK).setCellValue("1");createCell(row, 1, styles.get(CellStyleKey.CELL_NORMAL), Cell.CELL_TYPE_BLANK).setCellValue("1");createCell(row, 2, styles.get(CellStyleKey.CELL_NORMAL), Cell.CELL_TYPE_BLANK).setCellValue("1");FileOutputStream FileOutputStream = new FileOutputStream("D:/test/encrty.xlsx");xssfWorkbook.write(FileOutputStream);enctypt(FileOutputStream);xssfWorkbook.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void enctypt(FileOutputStream FileOutputStream){try {POIFSFileSystem fs = new POIFSFileSystem();EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);Encryptor enc = info.getEncryptor();enc.confirmPassword("foobaa");OPCPackage opc = OPCPackage.open(new File("D:/test/encrty.xlsx"), PackageAccess.READ_WRITE);//OPCPackage opc = OPCPackage. .create(FileOutputStream);OutputStream os = enc.getDataStream(fs);opc.save(os);opc.close();FileOutputStream fos = new FileOutputStream("D:/test/encrty.xlsx");fs.writeFilesystem(fos);fos.close();} catch (Exception e) {e.printStackTrace();} }private static SXSSFCell createCell(SXSSFRow row, int cellindex, CellStyle style, int cellType) {SXSSFCell cell = row.createCell(cellindex, cellType);cell.setCellStyle(style);return cell;
}private static Map<String, CellStyle> createStyles(SXSSFWorkbook xssfWorkbook) {Map<String, CellStyle> styles = new HashMap<String, CellStyle>();CreationHelper creationHelper = xssfWorkbook.getCreationHelper();DataFormat df = creationHelper.createDataFormat();CellStyle style = createBorderedStyle(xssfWorkbook);Font headerFont = xssfWorkbook.createFont();headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);headerFont.setFontName("Consolas");headerFont.setFontHeightInPoints((short) 12);style.setAlignment(CellStyle.ALIGN_CENTER);style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);style.setFont(headerFont);styles.put(CellStyleKey.HEADER, style);Font font = xssfWorkbook.createFont();font.setBoldweight(Font.BOLDWEIGHT_BOLD);font.setFontName("Consolas");font.setFontHeightInPoints((short) 12);style = createBorderedStyle(xssfWorkbook);style.setAlignment(CellStyle.ALIGN_LEFT);style.setFont(font);styles.put(CellStyleKey.CELL_BOLD, style);style = createBorderedStyle(xssfWorkbook);style.setAlignment(CellStyle.ALIGN_LEFT);style.setWrapText(true);styles.put(CellStyleKey.CELL_NORMAL, style);style = createBorderedStyle(xssfWorkbook);style.setAlignment(CellStyle.ALIGN_RIGHT);style.setWrapText(true);style.setDataFormat(df.getFormat("yyyy-MM-dd"));styles.put(CellStyleKey.CELL_NORMAL_DATE, style);style = createBorderedStyle(xssfWorkbook);style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);styles.put(CellStyleKey.CELL_BG_BLUE, style);return styles;
}interface CellStyleKey {public final static String HEADER = "header";public final static String CELL_BOLD = "cell_b";public final static String CELL_NORMAL = "cell_normal";public final static String CELL_NORMAL_DATE = "cell_normal_date";public final static String CELL_BG_BLUE = "cell_blue";
}private static CellStyle createBorderedStyle(SXSSFWorkbook xssfWorkbook) {CellStyle style = xssfWorkbook.createCellStyle();style.setBorderRight(CellStyle.BORDER_THIN);style.setRightBorderColor(IndexedColors.BLACK.getIndex());style.setBorderBottom(CellStyle.BORDER_THIN);style.setBottomBorderColor(IndexedColors.BLACK.getIndex());style.setBorderLeft(CellStyle.BORDER_THIN);style.setLeftBorderColor(IndexedColors.BLACK.getIndex());style.setBorderTop(CellStyle.BORDER_THIN);style.setTopBorderColor(IndexedColors.BLACK.getIndex());return style;
}interface CellStyleKey1 {public final static String HEADER = "header";public final static String CELL_BOLD = "cell_b";public final static String CELL_NORMAL = "cell_normal";public final static String CELL_NORMAL_DATE = "cell_normal_date";public final static String CELL_BG_BLUE = "cell_blue";
}private static SXSSFSheet createWorksheet(SXSSFWorkbook xssfWorkbook, String sheetName) {if(StringUtils.isBlank(sheetName)){sheetName = "" + System.currentTimeMillis();}SXSSFSheet sheet = xssfWorkbook.createSheet(sheetName);sheet.setDisplayGridlines(false);sheet.setPrintGridlines(false);sheet.setFitToPage(true);sheet.setHorizontallyCenter(true);sheet.setDefaultColumnWidth(20);return sheet;
}
}
apache poi excel word 加密,不用借助其他jxcell.jar包相关推荐
- [实用][更新中]Java Apache POI 打印Word文档工具(含文本替换,动态表格功能)
[实用][更新中]Java Apache POI 打印Word文档工具(含文本替换,动态表格功能) 基于Apache POI对Word进行操作 一.基于Apache POI封装的word文档工具V1. ...
- apache poi使用例_使用java Apache poi 根据word模板生成word报表例子
[实例简介] 使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持. 使用说明:https://b ...
- java word apache poi 操作word模板。
apache poi 操作word模板. 操作方式: 1.对于固定格,可以遍历格子然后替换其中指定的值例如在要替换的cell写入${example} 这样格式,遍历到之后替换. 2.对于需要增长的表格 ...
- 使用java Apache poi 根据word模板生成word报表
使用java Apache poi 根据word模板生成word报表 使用poi读取word模板,替换word中的{text}标签,并根据自定义标签循环生成表格或表格中的行. 代码示例下载:https ...
- JAVA - 使用Apache POI生成word(二) 设置纸张大小、调整纸张方向
JAVA - 使用Apache POI生成word(二) 设置纸张大小.调整纸张方向 前言 之前开发时,需要将纸张方向由纵向改为横向,查询资料得出只需要设置一下纸张的长度与宽度便可实现相同的效果. 1 ...
- 关于Apache / poi 生成word文档之后不能正常换行的问题
近期公司项目有个把文本转成word文档的功能,开始使用io操作输出文件的方式(后缀名是docx),使用手机自带的文档浏览工具打开是没有问题的,但是在电脑上用微软office就打开有问题了,于是找了三方 ...
- JAVA - 使用Apache POI生成word(三)设置页边距
JAVA - 使用Apache POI生成word(三)设置页边距 1. pom引入依赖 <dependency><groupId>org.apache.poi</gro ...
- Apache POI Excel、WORD、PowerPoint、Visio处理
一 :简介 开发中经常会设计到excel的处理,如导出Excel,导入Excel到数据库中,操作Excel目前有两个框架,一个是apache 的poi, 另一个是 Java Excel Apache ...
- php+xls加密,POI Excel xlsx加密
POI 操作Excel 加密. POI版本: org.apache.poi poi 3.10.1 org.apache.poi poi-ooxml 3.10.1 示例代码: import java.i ...
- org.apache.poi往word模板中填充数据(word2003)
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.通过字面意思,我们大概知道这个API是可以用来做 j ...
最新文章
- ASP.NET中实现大结果集分页研讨 转
- jmeter 导入java_8. Jmeter导入jar包
- FreeRTOS实验_独立看门狗监视多线程
- 单片机 PWM输出 c
- 萤石云平台接入_前端接入监控视频
- 关于扫描的一个比较好的网站
- xcode 左侧导航栏 no finder results 问题的解决方法
- Spring MVC:表单处理卷。 2 –复选框处理
- 【Android游戏开发之一】设置全屏以及绘画简单的图形
- android怎么让图片显示在button上面_opencv怎么样可以实时显示图片HSV值
- android driver log,Android调试驱动抓log的方法
- 金格HTML签章集成
- 2019 CUMCM ABC Notes
- Manjaro安装Nvidia显卡驱动失败或者启动黑屏卡死的原因以及Nvidia Prime与Intel核显切换方法
- 智慧交通篇 2 —— 公交刷卡(非接触IC卡)系统全透析
- 全国地址json android,全国城市+四级城市地址+邮编+区号+经纬度json版
- 图片转换成代码的小工具Image2Lcd使用说明
- 厦门大学和福州大学计算机专业哪个好,福建最好的5所大学,除了厦门大学,你还知道哪所大学?...
- 培养架构思维成为优秀的架构师
- 软件工程毕业设计课题(21)基于JAVA毕业设计电子书小说网站系统毕设作品项目