Java读取Excel和写入Excel(兼容xls和xlsx格式)

需要引入poi的相关jar包即可。

1.ExcelHandle 判断Excel的格式

import java.io.File;

import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ExcelHandle {
private static final String EXCEL_XLS = "xls";  
private static final String EXCEL_XLSX = "xlsx";  
 
/** 
     * Judging the version of Excel
     * @param in 
     * @param filename 
     * @return 
     * @throws IOException 
     */  
    public static Workbook getWorkbook(File file) throws IOException{  
        Workbook wb = null;  
        FileInputStream in = new FileInputStream(file);  
        if(file.getName().endsWith(EXCEL_XLS)){  //Excel 2003  
            wb = new HSSFWorkbook(in);  
            System.out.println("Excel file type:xls");
        }else if(file.getName().endsWith(EXCEL_XLSX)){  // Excel 2007/2010  
            wb = new XSSFWorkbook(in); 
            System.out.println("Excel file type:xlsx");
        }  
        return wb;  
    }  
}

2.ParseExcel用来解析Excel,一行一行读取

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


public class ParseExcel{

/** 
     * Read Excel test, compatible with Excel 2003/2007/2010
     * @throws Exception  
     */  
public static void parse_excel(Workbook workbook) {
int sheetCount = workbook.getNumberOfSheets(); // Total number of sheets
  
         for(int s = 0; s < sheetCount; s++) {
        System.out.println("————————————————————————Start processing sheet"+(s+1)+"————————————————————————");
        String sheetname = workbook.getSheetName(s);
        System.out.println("Name of sheet"+(s+1)+":"+sheetname);
        //Set the subscript of the excel sheet: 0 start
             Sheet sheet = workbook.getSheetAt(s);//The first sheet  
        // Set the count to skip the first line of the directory
             int count = 0;  
             //Total number of rows
             int rowLength = sheet.getLastRowNum()+1;  
             System.out.println("Total number of rows:"+rowLength);
             //Get the first line
             Row rowzero = sheet.getRow(0); 
             //Total number of columns 
             int colLength = rowzero.getLastCellNum(); 
             System.out.println("Total number of columns:"+colLength);
             for (int i = 0; i < rowLength;i++) {
                 Row row = sheet.getRow(i);
                 for (int j = 0; j < colLength; j++) {  
                Cell cell = row.getCell(j);
                if(cell==null || "".equals(cell.toString())){  
                    System.out.print("null"+" ");
                     }else{
                    System.out.print(cell.toString()+" ");
                     }
                 }  
                 System.out.println();
             }  
             System.out.println("————————————————————————sheet"+(s+1)+" processing ends————————————————————————");
         }
}

}

3.OutputToExcel往Excel写入数据

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


public class OutputToExcel{
public static void outputtoexcel(Workbook workbook) {  
OutputStream out = null;  
int sheetCount = workbook.getNumberOfSheets(); //Total number of sheets 
        try {  
        for(int s = 0; s < sheetCount; s++) {
        System.out.println("————————————————————————Start processing sheet"+(s+1)+"————————————————————————");
            String sheetname = workbook.getSheetName(s);
            System.out.println("Name of sheet"+(s+1)+":"+sheetname);
            //Set the subscript of the excel sheet: 0 start
                 Sheet sheet = workbook.getSheetAt(s);//The first sheet  
                 // Set the count to skip the first line of the directory
                 int count = 0;  
                 //Total number of rows
                 int rowLength = sheet.getLastRowNum()+1;  
                 System.out.println("Total number of rows:"+rowLength);
                 //Get the first line  
                 Row rowzero = sheet.getRow(0); 
                 //Total number of columns  
                 int colLength = rowzero.getLastCellNum(); 
                 System.out.println("Total number of columns:"+colLength);
                 //Determine if the result column already exists, create it without
                 if("Result".equals(rowzero.getCell(colLength-1).getStringCellValue())) {
                colLength = colLength-1;
                 }else {
                  /** 
          * Write a new column to the sheet
          */
                 Cell newcell = rowzero.createCell(colLength);  
                 newcell.setCellValue("Result");  
                 }
               
                /* 
                 for (int i = 0; i < rowLength;i++) {
                     Row row = sheet.getRow(i);
                     for (int j = 0; j < colLength; j++) {
                    Cell cell = row.getCell(j);
                    if(cell==null || "".equals(cell.toString())){  
                        System.out.print("null"+" ");
                         }else{
                        System.out.print(cell.toString()+" ");
                         }
                     }  
                     System.out.println();
                 }  */
                 int writeflag = 1;//Line number written
                 for(int j = 0 ; j < (rowLength-1)/15; j++) {
                /** 
          * Write results back to sheet
          */
                     Row writerow = sheet.getRow(writeflag);
                     Cell writecell = writerow.createCell(colLength);  
                     writecell.setCellValue("true");
                     writeflag+=15;
                 }
                 // Create file output and prepare output table
            out =  new FileOutputStream("C:\\Users\\test.xlsx");  
            workbook.write(out); 
         System.out.println("Data written successfully");  
             System.out.println("————————————————————————sheet"+(s+1)+" processing ends————————————————————————");
        }
         
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            try {  
                if(out != null){  
                    out.flush();  
                    out.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }
}

}

4.测试方法

public static void main(String[] args) {
    
// TODO Auto-generated method stub
  try {

  File file = new File("C:\\Users\\test.xlsx");  
  Workbook workbook = ExcelHandle.getWorkbook(file);
  OutputToExcel.outputtoexcel(workbook);
  //ParseExcel.parse_excel(workbook);
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
}

猜你喜欢

转载自blog.csdn.net/u011213317/article/details/79911560
今日推荐