📜  使用 Apache POI 在Java中的 Excel 文件中创建工作表

📅  最后修改于: 2021-10-19 05:17:38             🧑  作者: Mango

对于给定的 Excel 文件(此处为Geeks.xlsx ),需要在其中创建工作表。

脚步:

  1. 创建Java Maven 项目
  2. 在 pom.xml 文件中添加依赖
    
    
          org.apache.poi
          poi
          3.12
        
        
          org.apache.poi
          poi-ooxml
          3.12
        
    
  3. 在 javaResource 文件夹中创建一个类
    import java.io.*;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
      
    public class CreateSheet {
        public static void main(String[] args) 
          throws FileNotFoundException, IOException
        {
      
            // Creating Workbook instances
            Workbook wb = new HSSFWorkbook();
      
            // An output stream accepts output bytes and sends them to sink.
            OutputStream fileOut = new FileOutputStream("Geeks.xlsx");
              
            // Creating Sheets using sheet object
            Sheet sheet1 = wb.createSheet("Array");
            Sheet sheet2 = wb.createSheet("String");
            Sheet sheet3 = wb.createSheet("LinkedList");
            Sheet sheet4 = wb.createSheet("Tree");
            Sheet sheet5 = wb.createSheet("Dynamic Programing");
            Sheet sheet6 = wb.createSheet("Puzzles");
              
      
    System.out.println("Sheets Has been Created successfully");
      
            wb.write(fileOut);
        }
    }
    

输出:

Sheets Has been Created successfully

这里在 geeks.xlsx 文件中创建了 6 张表: