java_I/O操作_CreateEXcelFile

Posted -chang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java_I/O操作_CreateEXcelFile相关的知识,希望对你有一定的参考价值。

  1. //主函数
  2. public class MainDemo {
  3. public static void main(String[] args) throws Exception {
  4. ExcelDocument excelDoc = new ExcelDocument();
  5. excelDoc.createExcelFile07();
  6. }
  7. }

 

 

  1. //封装Students类
  2. import java.util.Date;
  3. import javax.xml.crypto.Data;
  4. public class Students {
  5. private String code;
  6. private String name;
  7. private Integer age;
  8. private Date birth;
  9. public Students(String code,String name,Integer age,Date birth) {
  10. this.code = code;
  11. this.name = name;
  12. this.age = age;
  13. this.birth = birth;
  14. }
  15. public String getCode() {
  16. return code;
  17. }
  18. protected void setCode(String code) {
  19. this.code = code;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. protected void setName(String name) {
  25. this.name = name;
  26. }
  27. public Integer getAge() {
  28. return age;
  29. }
  30. protected void setAge(Integer age) {
  31. this.age = age;
  32. }
  33. public Date getBirth() {
  34. return birth;
  35. }
  36. protected void setBirth(Date birth) {
  37. this.birth = birth;
  38. }
  39. }

 

  1. //创建Students类实例并输出到Excel中
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import javax.jws.soap.SOAPBinding.Style;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.ss.usermodel.CellStyle;
  15. import org.apache.poi.ss.usermodel.Row;
  16. import org.apache.poi.ss.usermodel.Sheet;
  17. import org.apache.poi.ss.usermodel.Workbook;
  18. import org.apache.poi.xssf.usermodel.XSSFCell;
  19. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  20. import org.apache.poi.xssf.usermodel.XSSFRow;
  21. import org.apache.poi.xssf.usermodel.XSSFSheet;
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  23. public class ExcelDocument {
  24. //新建1997版Excel文档
  25. public static Workbook createExcelFile97() {
  26. Workbook wb = new HSSFWorkbook();
  27. return wb;
  28. }
  29. //新建2007版Excel文档
  30. public static void createExcelFile07() throws Exception {
  31. XSSFWorkbook wb = new XSSFWorkbook();
  32. XSSFSheet sheet = wb.createSheet("学生表");//第二步:在workbook中添加一张sheet表
  33. XSSFRow row = sheet.createRow((int)0); //第三步:在sheet表中添加一行作为表头
  34. XSSFCellStyle style = (XSSFCellStyle)createStyle(wb);//第四步:设置样式
  35. //第五步:创建cell表头
  36. XSSFCell cell = row.createCell((short)0);
  37. cell.setCellValue("学号");
  38. cell.setCellStyle(style);
  39. cell = row.createCell((short)1);
  40. cell.setCellValue("姓名");
  41. cell.setCellStyle(style);
  42. cell = row.createCell((short)2);
  43. cell.setCellValue("年龄");
  44. cell.setCellStyle(style);
  45. cell = row.createCell((short)3);
  46. cell.setCellValue("生日");
  47. cell.setCellStyle(style);
  48. //第六步:写入实体数据
  49. List list = getStudents();
  50. for (int i = 0; i < list.size(); i++) {
  51. row = sheet.createRow((int)i+1);
  52. Students stu = (Students)list.get(i);
  53. row.createCell((int)0).setCellValue(stu.getCode());
  54. row.createCell((int)1).setCellValue(stu.getName());
  55. row.createCell((int)2).setCellValue(stu.getAge());
  56. row.createCell((int)3).setCellValue(stu.getBirth());
  57. }
  58. //第七步:将文件写入指定的位置
  59. FileOutputStream outStream = getFileOutputStream();
  60. wb.write(outStream);
  61. outStream.close();
  62. }
  63. //将文件写入指定的位置
  64. private static FileOutputStream getFileOutputStream() throws FileNotFoundException {
  65. String path = "E:" + File.separatorChar + "Members.xls";
  66. File file = new File(path);
  67. if (!file.getParentFile().exists()) { //如果父级路径不存在就创建父级目录
  68. file.getParentFile().mkdirs();
  69. if (!file.exists()) { //如果文件不存在就创建文件
  70. try {
  71. file.createNewFile();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77. FileOutputStream outputStream = new FileOutputStream(file);
  78. return outputStream;
  79. }
  80. //创建cell的样式
  81. private static CellStyle createStyle(Workbook wb) {
  82. CellStyle style= wb.createCellStyle();
  83. style.setAlignment(CellStyle.ALIGN_CENTER);//格式居中
  84. return style;
  85. }
  86. //创建数组
  87. private static List<Students> getStudents() throws Exception {
  88. List list = new ArrayList();
  89. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
  90. Students student1 = new Students("1", "张三", (Integer)24, dateFormat.parse("1993-08-28"));
  91. Students student2 = new Students("2", "李思", (Integer)23, dateFormat.parse("1994-03-07"));
  92. list.add(student1);
  93. list.add(student2);
  94. return list;
  95. }
  96. }

POI.jar链接:https://pan.baidu.com/s/1KGcwHJHfWUwLQifOJS5XQg 密码:xvd2

以上是关于java_I/O操作_CreateEXcelFile的主要内容,如果未能解决你的问题,请参考以下文章

java_I/O_合并流(SequenceInputStream)

Java_I/O输入输出_实现当用户输入姓名和密码时,将每一个姓名和密码加在文件中,如果用户输入done,就结束程序。

上下文操作栏与工具栏操作混淆

RxJava操作符 __结合操作

hibernate_06_单表操作_增删改操作

LINUX的一些常用操作