[Java] POI操作excel表格(建立工作薄、创建工作表、将数据填充到单元格中) →→→→→进入此内容的聊天室

来自 , 2020-08-20, 写在 Java, 查看 143 次.
URL http://www.code666.cn/view/99bcfcd7
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package excel;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import java.io.FileInputStream;
  12. import org.apache.poi.hssf.usermodel.HSSFCell;
  13. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  14. import org.apache.poi.hssf.usermodel.HSSFRow;
  15. /**
  16.  *
  17.  * @author Administrator
  18.  */
  19. public class ExcelUtil
  20. {
  21.     public String filePath = "e:\\workbook.xls";
  22.     public void newWordBook()
  23.     {
  24.         HSSFWorkbook wb = new HSSFWorkbook();
  25.         try
  26.         {
  27.             FileOutputStream fileOut = new FileOutputStream (filePath);
  28.             wb.write (fileOut);
  29.             fileOut.close();
  30.         }
  31.         catch (FileNotFoundException ex)
  32.         {
  33.             System.out.println (ex.getMessage() );
  34.         }
  35.         catch (IOException ex)
  36.         {
  37.             System.out.println (ex.getMessage() );
  38.         }
  39.     }
  40.     /**
  41.      * 创建空白文件
  42.      */
  43.     public void newSheet()
  44.     {
  45.         HSSFWorkbook wb = new HSSFWorkbook();
  46.         wb.createSheet ("第一页");
  47.         wb.createSheet ("第二页");
  48.         try
  49.         {
  50.             FileOutputStream fileOut = new FileOutputStream (filePath);
  51.             wb.write (fileOut);
  52.             fileOut.close();
  53.         }
  54.         catch (FileNotFoundException ex)
  55.         {
  56.             System.out.println (ex.getMessage() );
  57.         }
  58.         catch (IOException ex)
  59.         {
  60.             System.out.println (ex.getMessage() );
  61.         }
  62.     }
  63.     private void saveWorkBook (HSSFWorkbook wb)
  64.     {
  65.         try
  66.         {
  67.             FileOutputStream fileOut = new FileOutputStream (filePath);
  68.             wb.write (fileOut);
  69.             fileOut.close();
  70.         }
  71.         catch (FileNotFoundException ex)
  72.         {
  73.             System.out.println (ex.getMessage() );
  74.         }
  75.         catch (IOException ex)
  76.         {
  77.             System.out.println (ex.getMessage() );
  78.         }
  79.     }
  80.     private HSSFWorkbook getWorkBook (String filePath)
  81.     {
  82.         try
  83.         {
  84.             FileInputStream fileIn = new FileInputStream (filePath);
  85.             HSSFWorkbook wb = new HSSFWorkbook (fileIn);
  86.             fileIn.close();
  87.             return wb;
  88.         }
  89.         catch (IOException ex)
  90.         {
  91.             System.out.println (ex.getMessage() );
  92.             return null;
  93.         }
  94.     }
  95.     private HSSFCell getCell (HSSFSheet sheet, int rowIndex, short columnIndex)
  96.     {
  97.         HSSFRow row = sheet.getRow (rowIndex);
  98.         if (row == null)
  99.         {
  100.             row = sheet.createRow (rowIndex);
  101.         }
  102.         HSSFCell cell = row.getCell (columnIndex);
  103.         if (cell == null)
  104.         {
  105.             cell = row.createCell ( (short) columnIndex);
  106.         }
  107.         return cell;
  108.     }
  109.     /**
  110.      * 写数据
  111.      * @param file
  112.      */
  113.     public void writeData (String file)
  114.     {
  115.         //创建工作薄
  116.         HSSFWorkbook wb = getWorkBook (file);
  117.         if (wb == null)
  118.         {
  119.             return;
  120.         }
  121.         //获取工作表
  122.         HSSFSheet sheet = wb.getSheetAt (0);
  123.         if (sheet == null)
  124.         {
  125.             sheet = wb.createSheet ("第一页");
  126.         }
  127.         HSSFCell cell = getCell (sheet, 0, (short) 0);
  128.         //数值
  129.         cell.setCellValue (123);
  130.         //字符串
  131.         HSSFRichTextString str = new HSSFRichTextString ("你好");
  132.         cell = getCell (sheet, 0, (short) 1);
  133.         cell.setCellValue (str);
  134.         //保存
  135.         saveWorkBook (wb);
  136.     }
  137.     public static void main (String[] args)
  138.     {
  139.         ExcelUtil excel = new ExcelUtil();
  140.         excel.writeData (excel.filePath);
  141.     }
  142. }

回复 "POI操作excel表格(建立工作薄、创建工作表、将数据填充到单元格中)"

这儿你可以回复上面这条便签

captcha