1. 程式人生 > >Java讀取txt文字內容並寫入Excel

Java讀取txt文字內容並寫入Excel

本文實現的是將txt檔案中的內容寫入到excel中

(注:依賴Jar包:jxl.jar,請自行下載)

程式碼:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class Test
{
    //讀取的txt檔案路徑
    private static String txtFilePath = "C:\\Users\\Administrator\\Desktop\\新建文字文件.txt";
    
    //生成的excel檔案路徑
    private static String excelFilePath = new StringBuffer().append("C:/Users/Administrator/Desktop/")
            .append("資料").append(new SimpleDateFormat("YYYYMMdd").format(new Date())).append(".xls").toString();
    
    //編碼格式
    private static String encoding = "GBK";
    
    public static void readAndWrite(String filePath)
    {
        try
        {
            File file = new File(filePath);


            File tempFile = new File(excelFilePath);
            
            //判斷檔案是否存在
            if (file.isFile() && file.exists())
            {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考慮到編碼格式


                BufferedReader bufferedReader = new BufferedReader(read);
                
                WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
                
                WritableSheet sheet = workbook.createSheet("Sheet1", 0);
                
                //一些臨時變數,用於寫到excel中
                Label l = null;
                
                String lineTxt = null;
                
                //設定字型為宋體,11號
                WritableFont headerFont = new WritableFont(WritableFont.createFont("宋體"), 11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
                
                WritableCellFormat headerFormat = new WritableCellFormat (headerFont);
                
                int column = 0;
                int i = 0;
                
                while ((lineTxt = bufferedReader.readLine()) != null)
                {
                    l = new Label(column++, i, lineTxt, headerFormat);
                    sheet.addCell(l);
                    
                    //判斷內容是否為空行,如果是,則轉行
                    if("".equals(lineTxt))
                    {
                        i++;
                        column = 0;
                        continue;
                    }
                }
                //設定單元格寬度
                sheet.setColumnView(0, 20);
                sheet.setColumnView(1, 35);
                sheet.setColumnView(2, 15);
                
                //寫入檔案
                workbook.write();
                //關閉檔案
                workbook.close();
                read.close();
            }
            else
            {
                System.out.println("找不到指定的檔案");
            }
        }
        catch (Exception e)
        {
            System.out.println("讀取檔案內容出錯");
            e.printStackTrace();
        }


    }


    public static void main(String argv[])
    {
        readAndWrite(txtFilePath);
    }
}