1. 程式人生 > >讀取excel檔案內容輸入到另外一個檔案

讀取excel檔案內容輸入到另外一個檔案

package com.example.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class QueryExcel {
public static void main(String[] args) {

    // 讀取Excel檔案
    File file = new File("D:/info.xls");
    try {
        //得到所有資料
        List<List<String>> allData=readExcel(file);
        //直接將它寫到excel中
        makeExcel(allData);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
private static List<List> readExcel(File file) throws Exception {

        // 建立輸入流,讀取Excel
        InputStream is = new FileInputStream(file.getAbsolutePath());
        // jxl提供的Workbook類
        Workbook wb = Workbook.getWorkbook(is);
        // 只有一個sheet,直接處理
        //建立一個Sheet物件
        Sheet sheet = wb.getSheet(0);
        // 得到所有的行數
        int rows = sheet.getRows();
        // 所有的資料
        List<List<String>> allData = new ArrayList<List<String>>();
        // 越過第一行 它是列名稱
        for (int j = 1; j < rows; j++) {

            List<String> oneData = new ArrayList<String>();
            // 得到每一行的單元格的資料
            Cell[] cells = sheet.getRow(j);
            for (int k = 0; k < cells.length; k++) {

                oneData.add(cells[k].getContents().trim());
            }
            // 儲存每一條資料
            allData.add(oneData);
            // 打印出每一條資料
            System.out.println(oneData);

        }
        return allData;

    }
 public static  void makeExcel(List<List<String>> result) {
        //第一步,建立一個workbook對應一個excel檔案
        HSSFWorkbook workbook = new HSSFWorkbook();
        //第二部,在workbook中建立一個sheet對應excel中的sheet
        HSSFSheet sheet = workbook.createSheet("測試結果");
        //第三部,在sheet表中新增表頭第0行,老版本的poi對sheet的行列有限制
        HSSFRow row = sheet.createRow(0);
        //第四步,建立單元格,設定表頭
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("學號");
        cell = row.createCell(1);
        cell.setCellValue("姓名");
        cell = row.createCell(2);
        cell.setCellValue("使用者名稱");
        cell = row.createCell(3);
        cell.setCellValue("密碼");
        cell = row.createCell(4);
        cell.setCellValue("住址");
        cell = row.createCell(5);
        cell.setCellValue("result");
        //第五步,寫入資料
        for(int i=0;i<result.size();i++) {
            List<String> oneData = result.get(i);
            HSSFRow row1 = sheet.createRow(i + 1);
            for(int j=0;j<oneData.size();j++) {
                 //建立單元格設值
                row1.createCell(j).setCellValue(oneData.get(j));
            }
        }

        //將檔案儲存到指定的位置
        try {
            FileOutputStream fos = new FileOutputStream("E:\\result.xls");
            workbook.write(fos);
            System.out.println("寫入成功");
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}