最近简单的弄了下poi对excel的应用,为方便自己以后的使用就把一些基本操作记录下来,其他更复杂的操作可以等以后有需求的时候再来深入了解一番!

写操作:

  1. /**
  2. *
  3. * 层次结构就是workbook-->Sheet-->Row-->Cell
  4. * 只要按照这种层次结构操作就不会有什么大的问题
  5. * @author Administrator
  6. *
  7. */
  8. public class Test1 {
  9. public static void main(String args[]) throws IOException {
  10. //HSSFWorkbook对应的是2003
  11. //XSSFWorkbook对应的是2007
  12. //对于03和07它们的操作都是差不多的,只是在需要用07的时候把相应的HSSF前缀改成XSSF前缀就可以了
  13. //第一步建一个工作簿,即workbook
  14. Workbook workbook = new HSSFWorkbook();
  15. //第二步建一个工作表单,急sheet
  16. Sheet sheet = workbook.createSheet("mysheet1");
  17. for (int i=0;i<5;i++) {
  18. //有了表单以后就是行Row了,
  19. Row row = sheet.createRow(i);
  20. for (int j=0;j<5;j++) {
  21. //有了row以后就是row上的一个个小的单元格了
  22. Cell cell = row.createCell(j);
  23. //给单元格添加内容
  24. cell.setCellValue("row"+(i+1)+",column"+(j+1));
  25. }
  26. }
  27. //建一个用于存放新建的excel的文件输出流
  28. OutputStream os = new FileOutputStream("file/test1.xls");
  29. //把形成的workbook写到一个输出流里面
  30. workbook.write(os);
  31. os.close();
  32. }
  33. }

读操作:

  1. public class Test4 {
  2. public static void main(String args[]) throws IOException {
  3. InputStream is = new FileInputStream("file/test1.xls");
  4. Workbook wb = new HSSFWorkbook(is);
  5. Sheet sheet = wb.getSheetAt(0);
  6. // 因为Row,Cell,Sheet都继承了java.lang.Iterator接口,所以可以用下面的方法来进行遍历
  7. for (Row row : sheet) {
  8. for (Cell cell : row) {
  9. int cellType = cell.getCellType();
  10. switch (cellType) {
  11. //在取数据的时候类型一定要取对,否则将抛出异常
  12. case Cell.CELL_TYPE_STRING:
  13. String str = cell.getRichStringCellValue().getString();
  14. System.out.println(str);// 对取得的数据的简单处理;
  15. break;
  16. case Cell.CELL_TYPE_BLANK:
  17. if (DateUtil.isCellDateFormatted(cell)) {
  18. System.out.println(cell.getDateCellValue());
  19. } else {
  20. System.out.println(cell.getNumericCellValue());
  21. }
  22. break;
  23. case Cell.CELL_TYPE_FORMULA:
  24. System.out.println(cell.getCellFormula());
  25. break;
  26. case Cell.CELL_TYPE_BOOLEAN:
  27. System.out.println(cell.getBooleanCellValue());
  28. break;
  29. default:
  30. System.out.println("---------------------");
  31. break;
  32. }
  33. }
  34. }
  35. //当然还可以这样来遍历
  36. //row是从1开始的,cell是从头0开始的,但是在创建的时候它们都是0-based;
  37. for (int i=0;i<sheet.getLastRowNum()+1;i++) {
  38. Row row = sheet.getRow(i);
  39. if (row==null)
  40. continue;
  41. for (int j=0;j<row.getLastCellNum();j++) {
  42. Cell cell = row.getCell(j);
  43. if (cell==null)
  44. continue;
  45. //在取数据的时候数据类型一定要取对,否则将抛出异常,like NumberFormatException
  46. System.out.println(cell.getStringCellValue());
  47. }
  48. }
  49. is.close();
  50. }
  51. }

合并单元格:

  1. public static void main(String args[]) throws IOException {
  2. Workbook wb = new HSSFWorkbook();
  3. Sheet sheet = wb.createSheet("sheet1");
  4. Row row = sheet.createRow(1);
  5. Cell cell = row.createCell(1);
  6. cell.setCellValue("a test of merge!");
  7. //执行合并操作的语句
  8. sheet.addMergedRegion(new CellRangeAddress(
  9. 1,// 开始行
  10. 1,// 结束行
  11. 1,// 开始列
  12. 3// 结束列
  13. ));
  14. OutputStream os = new FileOutputStream("file/test3.xls");
  15. wb.write(os);
  16. os.close();
  17. }

换行:

  1. public static void main(String args[]) throws IOException {
  2. Workbook wb = new HSSFWorkbook();
  3. Sheet sheet = wb.createSheet();
  4. Row row = sheet.createRow(6);
  5. sheet.autoSizeColumn(2);
  6. for (int i=0;i<5;i++) {
  7. Cell cell = row.createCell(i+2);
  8. CellStyle style = wb.createCellStyle();
  9. //to set cell newLine should set its wrap true
  10. style.setWrapText(true);
  11. //利用\n来实现换行操作,只有在Cell设置为setWrapText(true)的时候才能实现人为的换行
  12. cell.setCellValue("just use \n to wrap in a cell!");
  13. cell.setCellStyle(style);
  14. }
  15. OutputStream os = new FileOutputStream("file/test6_newLine.xls");
  16. wb.write(os);
  17. os.close();
  18. System.out.println("----------------------------");
  19. }

画图:

  1. //drawing shapes
  2. /*
  3. * To create a shape you have to go through the following steps:
  4. 1.Create the patriarch.
  5. 2.Create an anchor to position the shape on the sheet.
  6. 3.Ask the patriarch to create the shape.
  7. 4.Set the shape type (line, oval, rectangle etc...)
  8. 5.Set any other style details converning the shape. (eg: line thickness, etc...)
  9. */
  10. HSSFPatriarch partriarch = (HSSFPatriarch) sheet5.createDrawingPatriarch();
  11. HSSFSimpleShape shape = partriarch.createSimpleShape(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,5));
  12. shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
  13. //shape可以设置很多的属性
  14. shape.setFillColor(255,200,200);
  15. shape.setLineStyle(HSSFSimpleShape.LINESTYLE_DASHGEL);
  16. //Text boxes are created using a different call:

poi对excel的基本读写操作的更多相关文章

  1. java封装实现Excel建表读写操作

    对 Excel 进行读写操作是生产环境下常见的业务,网上搜索的实现方式都是基于POI和JXL第三方框架,但都不是很全面.小编由于这两天刚好需要用到,于是就参考手写了一个封装操作工具,基本涵盖了Exce ...

  2. Python Excel文件的读写操作(xlwt xlrd xlsxwriter)

    转:https://www.cnblogs.com/ultimateWorld/p/8309197.html Python语法简洁清晰,作为工作中常用的开发语言还是很强大的(废话). python关于 ...

  3. [python]使用xlrd对Excel表格进行读写操作

    2.1 导入模块 import xlrd 2.2 打开Excel文件读取数据 data = xlrd.open_workbook("excelFile.xls") 2.3 使用技巧 ...

  4. pandas对Excel文件的读写操作

    1.将Excel数据读为dataframe 1.1 直接读取 df = pd.read_excel('data.xlsx') 1.2 根据sheet索引 xls = pd.ExcelFile('dat ...

  5. python对excel文件的读写操作

    import xlrd,xlwt data = xlrd.open_workbook('a.xlsx') #读 table = data.sheets()[0] data_list = [] data ...

  6. spring boot 使用 POI 读取Excel文件

    内容简介 本文主要介绍使用POI进行Excel文件的相关操作,涉及读取文件,获取sheet表格,对单元格内容进行读写操作,以及合并单元格的操作. Excel文件目录 Excel模板文件存了resour ...

  7. python读取数据写入excel的四种操作

    Python对Excel的读写主要有:xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种 xlutils结合xlrd: 操作的是以xls后缀的excel,读取文件保留原格式 ...

  8. 使用POI来实现对Excel的读写操作

    事实上我感觉直接贴代码就好了.代码里面差点儿做到每一行一个凝视.应该看起来会比較简单 代码托管在github上:https://github.com/chsj1/ExcelUtils package ...

  9. Java学习---Excel读写操作

    1.1.1. 简介 Apache POI 使用Apache POI 完成Excel读写操作 Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API ...

随机推荐

  1. Go语言程序结构分析初探

    每一种编程语言都有自己的语法.结构以及自己的风格,这也是每种语言展现各自魅力及众不同的地方.Go也不例外,它简单而优雅,与此同时使用起来也很有趣.在本文中,我们将讨论以下几点: Go程序结构 如何运行 ...

  2. swagger 常用注解说明

    本内容引用自:https://blog.csdn.net/u014231523/article/details/76522486 常用注解: - @Api()用于类: 表示标识这个类是swagger的 ...

  3. JavaWeb学习 (十四)————JSP基础语法

    一.JSP模版元素 JSP页面中的HTML内容称之为JSP模版元素.  JSP模版元素定义了网页的基本骨架,即定义了页面的结构和外观. 二.JSP表达式 JSP脚本表达式(expression)用于将 ...

  4. httpd配置文件httpd.conf规则说明和一些基本指令

    apache httpd系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 本文主要介绍的是httpd的配置文件,包括一些最基本的指令.配置规 ...

  5. vue-cli脚手架搭建项目简单入门一

    搭建系统: Windows系统 简单了解Node.js.npm,安装Node.js,下载网址:http://nodejs.cn/download/ 查看node,npm安装成功与否.打开cmd命令行, ...

  6. Install/Remove of the Service Denied!

    在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld --install 报错: 信息如下一: Install/Remove of the Service Den ...

  7. html页面背景设定相关

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. border-sizing属性详解和应用

    box-sizing用于更改用于计算元素宽度和高度的默认的 CSS 盒子模型.它有content-box.border-box和inherit三种取值.inherit指的是从父元素继承box-sizi ...

  9. HashMap底层实现原理(JDK1.8)源码分析

    ref:https://blog.csdn.net/tuke_tuke/article/details/51588156 http://www.cnblogs.com/xiaolovewei/p/79 ...

  10. The open source JavaScript graphing library that powers Plotly

    https://plot.ly/javascript/time-series/ https://plot.ly/javascript/ https://github.com/plotly/plotly ...