我们知道要创建一张excel你得知道excel由什么组成,比如说sheet也就是一个工作表格,例如一行,一个单元格,单元格格式,单元格内容格式…这些都对应着poi里面的一个类。

一个excel表格:

HSSFWorkbook wb = new HSSFWorkbook();

一个工作表格(sheet):

HSSFSheet sheet = wb.createSheet(“测试表格”);

一行(row):

HSSFRow row1 = sheet.createRow(0);

一个单元格(cell):

HSSFCell cell2 = row2.createCell((short)0)

单元格格式(cellstyle):

HSSFCellStyle style4 = wb.createCellStyle()

单元格内容格式()

HSSFDataFormat format= wb.createDataFormat();

 

1:首先创建一个po对象

 

  1. package entity;
  2. public class Student {
  3. private int no;
  4. private String name;
  5. private int age;
  6. private String grage;
  7. public Student(int no, String name, int age, String grage) {
  8. super();
  9. this.no = no;
  10. this.name = name;
  11. this.age = age;
  12. this.grage = grage;
  13. }
  14. public int getNo() {
  15. return no;
  16. }
  17. public void setNo(int no) {
  18. this.no = no;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public int getAge() {
  27. return age;
  28. }
  29. public void setAge(int age) {
  30. this.age = age;
  31. }
  32. public String getGrage() {
  33. return grage;
  34. }
  35. public void setGrage(String grage) {
  36. this.grage = grage;
  37. }
  38. }

 

2实现导出的功能:

  1. 1 package demo;
  2. 2
  3. 3 import java.io.FileOutputStream;
  4. 4 import java.io.IOException;
  5. 5 import java.sql.SQLException;
  6. 6 import java.util.ArrayList;
  7. 7 import java.util.Date;
  8. 8 import java.util.List;
  9. 9
  10. 10 import org.apache.poi.hssf.usermodel.HSSFCell;
  11. 11 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. 12 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  13. 13 import org.apache.poi.hssf.usermodel.HSSFRow;
  14. 14 import org.apache.poi.hssf.usermodel.HSSFSheet;
  15. 15 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16. 16 import org.apache.poi.hssf.util.Region;
  17. 17 import org.apache.poi.ss.usermodel.Font;
  18. 18
  19. 19 import entity.Student;
  20. 20
  21. 21 public class Export_demo {
  22. 22 public static void main(String[] args) {
  23. 23 export();
  24. 24 }
  25. 25
  26. 26 public static void export(){
  27. 27 List<Student> studens=new ArrayList<Student>();
  28. 28 for (int i = 1; i <=20; i++) {
  29. 29 Student s=new Student(i, "a"+i, 20+i-20, "三年级");
  30. 30 studens.add(s);
  31. 31 }
  32. 32
  33. 33 HSSFWorkbook wb = new HSSFWorkbook();//创建一个excel文件
  34. 34 HSSFSheet sheet=wb.createSheet("学生信息");//创建一个工作薄
  35. 35 sheet.setColumnWidth((short)3, 20* 256); //---》设置单元格宽度,因为一个单元格宽度定了那么下面多有的单元格高度都确定了所以这个方法是sheet的
  36. 36 sheet.setColumnWidth((short)4, 20* 256); //--->第一个参数是指哪个单元格,第二个参数是单元格的宽度
  37. 37 sheet.setDefaultRowHeight((short)300); // ---->有得时候你想设置统一单元格的高度,就用这个方法
  38. 38 HSSFDataFormat format= wb.createDataFormat(); //--->单元格内容格式
  39. 39 HSSFRow row1 = sheet.createRow(0); //--->创建一行
  40. 40 // 四个参数分别是:起始行,起始列,结束行,结束列 (单个单元格)
  41. 41 sheet.addMergedRegion(new Region(0, (short) 0, 0, (short)5));//可以有合并的作用
  42. 42 HSSFCell cell1 = row1.createCell((short)0); //--->创建一个单元格
  43. 43 cell1.setCellValue("学生信息总览");
  44. 44
  45. 45
  46. 46 sheet.addMergedRegion(new Region(1, (short) 0, 1, (short)0));
  47. 47 HSSFRow row2= sheet.createRow(1); ////创建第二列 标题
  48. 48 HSSFCell fen = row2.createCell((short)0); //--->创建一个单元格
  49. 49 fen.setCellValue("编号/属性 ");
  50. 50 HSSFCell no = row2.createCell((short)1); //--->创建一个单元格
  51. 51 no.setCellValue("姓名 ");
  52. 52 HSSFCell age = row2.createCell((short)2); //--->创建一个单元格
  53. 53 age.setCellValue("年龄 ");
  54. 54 HSSFCell grage = row2.createCell((short)3); //--->创建一个单元格
  55. 55 grage.setCellValue("年级 ");
  56. 56
  57. 57 for (int i = 0; i <studens .size(); i++) {
  58. 58 sheet.addMergedRegion(new Region(1+i+1, (short) 0, 1+i+1, (short)0));
  59. 59 HSSFRow rows= sheet.createRow(1+i+1); ////创建第二列 标题
  60. 60 HSSFCell fens = rows.createCell((short)0); //--->创建一个单元格
  61. 61 fens.setCellValue(studens.get(i).getNo());
  62. 62 HSSFCell nos = rows.createCell((short)1); //--->创建一个单元格
  63. 63 nos.setCellValue(studens.get(i).getName());
  64. 64 HSSFCell ages = rows.createCell((short)2); //--->创建一个单元格
  65. 65 ages.setCellValue(studens.get(i).getAge());
  66. 66 HSSFCell grages = rows.createCell((short)3); //--->创建一个单元格
  67. 67 grages.setCellValue(studens.get(i).getGrage());
  68. 68 }
  69. 69 FileOutputStream fileOut = null;
  70. 70 try{
  71. 71 fileOut = new FileOutputStream("d:\\studens.xls");
  72. 72 wb.write(fileOut);
  73. 73 //fileOut.close();
  74. 74 System.out.print("OK");
  75. 75 }catch(Exception e){
  76. 76 e.printStackTrace();
  77. 77 }
  78. 78 finally{
  79. 79 if(fileOut != null){
  80. 80 try {
  81. 81 fileOut.close();
  82. 82 } catch (IOException e) {
  83. 83 // TODO Auto-generated catch block
  84. 84 e.printStackTrace();
  85. 85 }
  86. 86 }
  87. 87 }
  88. 88 }
  89. 89 }

效果图:

 

 3实现导入的功能:

  1. 1 package demo;
  2. 2
  3. 3 import java.io.FileInputStream;
  4. 4 import java.io.FileNotFoundException;
  5. 5 import java.io.InputStream;
  6. 6 import java.text.SimpleDateFormat;
  7. 7 import java.util.ArrayList;
  8. 8 import java.util.Date;
  9. 9 import java.util.List;
  10. 10
  11. 11 import org.apache.poi.hssf.usermodel.HSSFCell;
  12. 12 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  13. 13 import org.apache.poi.hssf.usermodel.HSSFRow;
  14. 14 import org.apache.poi.hssf.usermodel.HSSFSheet;
  15. 15 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16. 16 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  17. 17
  18. 18 import entity.Student;
  19. 19
  20. 20 public class Import_demo {
  21. 21 private static POIFSFileSystem fs;//poi文件流
  22. 22 private static HSSFWorkbook wb;//获得execl
  23. 23 private static HSSFRow row;//获得行
  24. 24 private static HSSFSheet sheet;//获得工作簿
  25. 25
  26. 26 public static void main(String[] args) throws FileNotFoundException {
  27. 27 InputStream in= new FileInputStream("d:\\studens.xls");
  28. 28 imports(in);
  29. 29 }
  30. 30
  31. 31 public static void imports(InputStream in ){
  32. 32 String str = "";
  33. 33 try {
  34. 34 fs = new POIFSFileSystem(in);
  35. 35 wb = new HSSFWorkbook(fs);
  36. 36 sheet=wb.getSheetAt(0);
  37. 37 //int rowfirst=sheet.getFirstRowNum();
  38. 38 int rowend=sheet.getLastRowNum();
  39. 39 for (int i = 2; i <=rowend; i++) {
  40. 40 row=sheet.getRow(i);
  41. 41 //System.out.println(row.get);
  42. 42 int colNum = row.getPhysicalNumberOfCells();//一行总列数
  43. 43 int j = 0;
  44. 44 while (j < colNum) {
  45. 45 str += getCellFormatValue(row.getCell((short) j)).trim() + "-";
  46. 46 j++;
  47. 47 }
  48. 48 System.out.println(str);
  49. 49 str="";
  50. 50 }
  51. 51 } catch (Exception e) {
  52. 52 // TODO: handle exception
  53. 53 }
  54. 54 }
  55. 55
  56. 56 private static String getCellFormatValue(HSSFCell cell) {
  57. 57 String cellvalue = "";
  58. 58 if (cell != null) {
  59. 59 // 判断当前Cell的Type
  60. 60 switch (cell.getCellType()) {
  61. 61 // 如果当前Cell的Type为NUMERIC
  62. 62 case HSSFCell.CELL_TYPE_NUMERIC:
  63. 63 case HSSFCell.CELL_TYPE_FORMULA: {
  64. 64 // 判断当前的cell是否为Date
  65. 65 if (HSSFDateUtil.isCellDateFormatted(cell)) {
  66. 66 Date date = cell.getDateCellValue();
  67. 67 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  68. 68 cellvalue = sdf.format(date);
  69. 69 }
  70. 70 // 如果是纯数字
  71. 71 else {
  72. 72 // 取得当前Cell的数值
  73. 73 cellvalue = String.valueOf(cell.getNumericCellValue());
  74. 74 }
  75. 75 break;
  76. 76 }
  77. 77 // 如果当前Cell的Type为STRIN
  78. 78 case HSSFCell.CELL_TYPE_STRING:
  79. 79 // 取得当前的Cell字符串
  80. 80 cellvalue = cell.getRichStringCellValue().getString();
  81. 81 break;
  82. 82 // 默认的Cell值
  83. 83 default:
  84. 84 cellvalue = " ";
  85. 85 }
  86. 86 } else {
  87. 87 cellvalue = "";
  88. 88 }
  89. 89 return cellvalue;
  90. 90 }
  91. 91
  92. 92 }

效果:

代码和jar宝下载路径

https://download.csdn.net/download/cengjianggh/10418815

posted on 2018-05-16 18:33 曾将 阅读() 评论() 编辑 收藏

版权声明:本文为GH0522原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/GH0522/p/9047472.html