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

转载于:https://www.cnblogs.com/-Chang/p/9441774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值