• Excelpoi导入导出--上完整代码!


     //1.写入excel
        Workbook();

    结果如下图: 详见代码文章末尾代码内方法: Workbook();

     //2.读取excel
            Readbook();

    结果如下图: 详见代码文章末尾代码内方法: Readbook();

       //3.写入车辆excel---------------该方法为实战用例
            Ceatbook();

    结果如下图: 详见代码文章末尾代码内方法:  Ceatbook();

     

        //4.list循环写入车辆excel---------------该方法为实战用例

    ForCeatbook();

    结果如下图: 详见代码文章末尾代码内方法:  ForCeatbook();

     

    1. public class Excelpoi {
    2. public static void main(String[] args) throws IOException {
    3. //1.写入excel
    4. Workbook();
    5. //2.读取excel
    6. Readbook();
    7. //3.写入车辆excel
    8. Ceatbook();
    9. //4.循环写入车辆excel
    10. ForCeatbook();
    11. }
    12. /**
    13. * 写入excel
    14. *
    15. * @param
    16. * @throws IOException
    17. */
    18. public static void Workbook() throws IOException {
    19. //1.打开excel
    20. XSSFWorkbook workbook = new XSSFWorkbook();
    21. //2.创建sheet页
    22. XSSFSheet sheet1 = workbook.createSheet("sheet1");//sheet:工作表
    23. //3.创建行,索引从0开始
    24. XSSFRow row = sheet1.createRow(0);//row 行
    25. //4.创建单元格,索引从0开始
    26. XSSFCell cell = row.createCell(0);//cell:单元格
    27. //5.向单元格中插入数据
    28. cell.setCellValue("你好");
    29. //6.将数据写入文件,相当于另存为
    30. workbook.write(new FileOutputStream("F:\\ExcelPoi\\excelpoi写入.xlsx"));
    31. System.out.println("生成excel成功");
    32. }
    33. /**
    34. * 读取excel
    35. *
    36. * @param
    37. * @throws IOException
    38. */
    39. public static void Readbook() throws IOException {
    40. //1.使用excel打开指定的xlsx文件
    41. XSSFWorkbook fw = new XSSFWorkbook("F:\\ExcelPoi\\excelpoi写入.xlsx");
    42. //2.获取第一个sheet页
    43. XSSFSheet sheet1 = fw.getSheetAt(0);
    44. //3.获取第0行
    45. XSSFRow row = sheet1.getRow(0);
    46. //4.获取第0个单元格
    47. XSSFCell cell = row.getCell(0);
    48. //5.获取单元格中的数据
    49. System.out.println(cell.getStringCellValue());
    50. System.out.println("读取成功");
    51. }
    52. /**
    53. * 创建一个车辆excel
    54. *
    55. * @throws IOException
    56. */
    57. public static void Ceatbook() throws IOException {
    58. /*1.创建EXCEL文件*/
    59. XSSFWorkbook workbook = new XSSFWorkbook();
    60. /*2.创建sheet页面*/
    61. XSSFSheet sheet1 = workbook.createSheet("sheet1");
    62. sheet1.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
    63. /*3.创建车辆信息头*/
    64. XSSFRow row = sheet1.createRow(0);
    65. XSSFCell cell = row.createCell(0);
    66. XSSFCellStyle textAlignCenter = workbook.createCellStyle();
    67. textAlignCenter.setAlignment(HorizontalAlignment.CENTER);//横着对其
    68. textAlignCenter.setVerticalAlignment(VerticalAlignment.CENTER);//上下对齐
    69. cell.setCellStyle(textAlignCenter);
    70. cell.setCellValue("车辆信息");
    71. /*4.创建表头*/
    72. XSSFRow titleRow = sheet1.createRow(1);
    73. XSSFCell idCell = titleRow.createCell(0);
    74. idCell.setCellValue("编号");
    75. XSSFCell nameCell = titleRow.createCell(1);
    76. nameCell.setCellValue("车牌");
    77. XSSFCell ageCell = titleRow.createCell(2);
    78. ageCell.setCellValue("颜色");
    79. /*5.创建数据*/
    80. XSSFRow dataRow1 = sheet1.createRow(2);
    81. XSSFCell idCell1 = dataRow1.createCell(0);
    82. idCell1.setCellValue("1");
    83. XSSFCell nameCell1 = dataRow1.createCell(1);
    84. nameCell1.setCellValue("京A11111");
    85. XSSFCell ageCell1 = dataRow1.createCell(2);
    86. ageCell1.setCellValue("black");
    87. XSSFRow dataRow2 = sheet1.createRow(3);
    88. XSSFCell idCell2 = dataRow2.createCell(0);
    89. idCell2.setCellValue("2");
    90. XSSFCell nameCell2 = dataRow2.createCell(1);
    91. nameCell2.setCellValue("京A22222");
    92. XSSFCell ageCell2 = dataRow2.createCell(2);
    93. ageCell2.setCellValue("red");
    94. workbook.write(new FileOutputStream("F:\\ExcelPoi\\车辆信息.xlsx"));
    95. System.out.println("车辆写入成功");
    96. }
    97. /**
    98. * 循环创建一个车辆excel
    99. *
    100. * @throws IOException
    101. */
    102. public static void ForCeatbook() throws IOException {
    103. /*1.创建EXCEL文件*/
    104. XSSFWorkbook workbook = new XSSFWorkbook();
    105. /*2.创建sheet页面*/
    106. XSSFSheet sheet1 = workbook.createSheet("sheet1");
    107. sheet1.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
    108. /*3.创建车辆信息头*/
    109. XSSFRow row = sheet1.createRow(0);
    110. XSSFCell cell = row.createCell(0);
    111. XSSFCellStyle textAlignCenter = workbook.createCellStyle();
    112. textAlignCenter.setAlignment(HorizontalAlignment.CENTER);
    113. textAlignCenter.setVerticalAlignment(VerticalAlignment.CENTER);
    114. cell.setCellStyle(textAlignCenter);
    115. cell.setCellValue("车辆信息");
    116. /*4.创建表头*/
    117. XSSFRow titleRow = sheet1.createRow(1);
    118. XSSFCell idCell = titleRow.createCell(0);
    119. idCell.setCellValue("编号");
    120. XSSFCell nameCell = titleRow.createCell(1);
    121. nameCell.setCellValue("车牌");
    122. XSSFCell ageCell = titleRow.createCell(2);
    123. ageCell.setCellValue("颜色");
    124. /*5.创建数据*/
    125. ArrayList vehicle = new ArrayList<>();
    126. vehicle.add(new Vehicle(1, "京A11111", "black"));
    127. vehicle.add(new Vehicle(2, "京A22222", "white"));
    128. vehicle.add(new Vehicle(3, "京A33333", "red"));
    129. vehicle.add(new Vehicle(4, "京A44444", "yellow"));
    130. for (int i = 0; i < vehicle.size(); i++) {
    131. XSSFRow dataRow1 = sheet1.createRow(2 + i);
    132. XSSFCell idCell1 = dataRow1.createCell(0);
    133. idCell1.setCellValue(vehicle.get(i).getId());
    134. XSSFCell nameCell1 = dataRow1.createCell(1);
    135. nameCell1.setCellValue(vehicle.get(i).getPlateNo());
    136. XSSFCell ageCell1 = dataRow1.createCell(2);
    137. ageCell1.setCellValue(vehicle.get(i).getColor());
    138. }
    139. workbook.write(new FileOutputStream("F:\\ExcelPoi\\循环车辆信息.xlsx"));
    140. System.out.println("循环车辆写入成功");
    141. }
    142. }

     

  • 相关阅读:
    老电脑升级内存、固态硬盘、重新装机过程记录
    “离职后,前老板让我回去改代码......”
    ise使用ChipScope时报错NgdBuild:604
    使用HTML制作静态网站:传统文化戏剧锡剧带psd设计图(2个页面)
    详解Python中哈希表的使用。站在开发者角度,与大家一起探究哈希的世界。
    Android:Canvas: trying to draw too large
    Makefile与CMake学习笔记
    Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
    window系统进行goolge代理配置(falcon proxy+burpsuite)
    基于两级分解和长短时记忆网络的短期风速多步组合预测模型
  • 原文地址:https://blog.csdn.net/tangyuanbaobao01/article/details/127870382