以下範例展示

1.如何取得Excel 所有的Sheet and Name

2.取得Excel 的Row and Column

3.取得每一個Sheet中的內容

4.搜尋每一個Sheet中,是否含有相同的字串

ReadExcelAPI.java
package tw.com.haoxiao.excel.read;

import java.io.File;
import java.util.Arrays;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ReadExcelAPI {
  // 檔案目錄
  private static String excelURL = "C:/ExcelTest.xls";
  // 搜尋的字串
  private static String searchString = "Apple";
 
  public static void main(String[] args)throws Exception {
    // 宣告File指向Excel的路徑
    File excelFile = new File(excelURL);
   
    // 透過 Workbook.getWorkbook 取得Excel
    Workbook workbook = Workbook.getWorkbook(excelFile);
   
    // 取得Excel 所有的Sheet Name
    System.out.println("======Sheet Name======");
    String[] sheetsByName = workbook.getSheetNames();
    System.out.println(Arrays.asList(sheetsByName));

    // 取得Excel 所有的Sheet
    System.out.println("======Sheet Name======");
    Sheet[] sheets = workbook.getSheets();
    for(int sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) {
      System.out.println("Index " + sheetIndex + " : " + sheets[sheetIndex].getName());
    }
   
    // 取得Rows and Columns 長度
    for(int sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) {
      int cols = sheets[sheetIndex].getColumns();
      int rows = sheets[sheetIndex].getRows();
      System.out.println("======" + sheets[sheetIndex].getName() + "======");
      System.out.println("Rows : " + rows);
      System.out.println("Cols : " + cols);
    }
   
    // 取得每一個Sheet的表格內容
    for(int sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) {
      int cols = sheets[sheetIndex].getColumns();
      int rows = sheets[sheetIndex].getRows();
      System.out.println("======" + sheets[sheetIndex].getName() + "======");
      for(int y = 0; y < rows; y++) {
        for(int x = 0; x < cols; x++) {
          System.out.print(sheets[sheetIndex].getCell(x, y).getContents() + "\t");
        }
        System.out.println();
      }
    }
   
    // 尋找 Cell的內容
    System.out.println("======搜尋======");
    for(int sheetIndex = 0; sheetIndex < sheets.length; sheetIndex++) {
      Sheet sheet = sheets[sheetIndex];
      Cell searchCell = sheet.findCell(searchString);
      if(searchCell != null) {
        System.out.println("找到" + searchString + "在" + sheet.getName()
            + "的(" + searchCell.getColumn() + ","
            + searchCell.getRow() + ")");
      }
    }
   
    // 關閉
    workbook.close();
  }
}

要使用Java讀取Excel也不難,只需要幾個步驟

1.建立要讀取Excel的路徑

2.透過Workbook.getWorkbook(File)取得Excel

3.取得Sheet

4.取得Sheet 中的 Cell

5.取得 Cell中的內容

6.關閉

 

ReadExcel.java
package tw.com.haoxiao.excel.read;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ReadExcel {
  private static String excelURL = "C:/ExcelTest.xls";
 
  public static void main(String[] args)throws Exception {
    // 宣告File指向Excel的路徑
    File excelFile = new File(excelURL);
   
    // 透過 Workbook.getWorkbook 取得Excel
    Workbook workbook = Workbook.getWorkbook(excelFile);
   
    // 取得Excel Sheet(頁籤)
    Sheet sheetByIndex = workbook.getSheet(0);
    Sheet sheetByName = workbook.getSheet("第一個頁籤");
   
    // 取得 Cell
    Cell cell1 = sheetByIndex.getCell(0, 0);
    Cell cell2 = sheetByIndex.getCell(1, 0);
    Cell cell3 = sheetByName.getCell(0, 1);
    Cell cell4 = sheetByName.getCell(1,1);
   
    // 取得 Cell的值
    String s1 = cell1.getContents();
    String s2 = cell2.getContents();
    String s3 = cell3.getContents();
    String s4 = cell4.getContents();
   
    // Print Cell Value
    System.out.println("Row 1 : " + s1 + "\t" + s2);
    System.out.println("Row 2 : " + s3 + "\t" + s4);
   
    // 關閉
    workbook.close();
  }

}

 
http://jexcelapi.sourceforge.net/ 下載 JExcelApi 的library
之後要使用Java建立一個Excel檔案就很簡單,只要幾個步驟。
1.建立一個File 指定Excel輸出的位置
2.透過Workbook.createWorkbook() 開啟一個Excel
3.產生一個Sheet, 並且命名
4.使用jxl,write.Label , Number, ... 建立每一個Cell的值
5.將 Cell 加入至 Sheet中
6.呼叫WritableWorkbook的write(), 將檔案寫入至硬碟中
7.關閉
WriteExcel.java
package tw.com.haoxiao.excel.write;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class WriteExcel {
  private static String excelURL = "C:/ExcelTest.xls";
 
  public static void main(String[] args)throws Exception {
    System.out.println("Create Excel to " + excelURL);
   
    // 建立輸入 Excel 檔案的目錄
    File execlFile = new File(excelURL);

    // 建立 Excel 檔, 此時只是暫存於記憶體中, 還沒有真正寫入於實體的硬碟中
    WritableWorkbook workbook = Workbook.createWorkbook(execlFile);
   
    // 建立一個頁籤, 以及名稱
    WritableSheet sheet = workbook.createSheet("第一個頁籤", 0);
   
    // 宣告 Cell 內的值, String, Number
    Label label1 = new Label(0,0,"Hello");
    Label label2 = new Label(1,0,"Excel");
    Label label3 = new Label(0,1,"pi=");
    Number number = new Number(1,1,3.14159);
   
    // 將 Cell 加入至 Sheet
    sheet.addCell(label1);
    sheet.addCell(label2);
    sheet.addCell(label3);
    sheet.addCell(number);
   
    // 將 Excel 寫入至硬碟中
    workbook.write();
   
    // 關閉
    workbook.close();
   
    System.out.println("Excel already create succes...");
  }
}

2008年8月9日 星期六

Java - I/O - 1

1.藉由I/O可以在程式執行過程中存取外部資料(如檔案、Socket、Console、記憶體等等)
2.File Class 不是一個標準的I/O類別,在java.io類別函式中也是唯一的non-stream class。不能讀取檔案、不能寫入檔案。
3.File Class 用於收集檔案(目錄)相關資訊,如建立檔案(並非寫入檔案內容)、修改檔名、設定/讀取檔案屬性、測試檢查檔案是否存在、列出檔案目錄等等

以下為 File 的部份操作實例
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package io;

import java.io.*;

/**
 *
 * @author Administrator
 */
public class NewClass {
    private static String path = "C:/haoxiao/io/";
    
    public static void main(String[] args)throws IOException {
        
        //建立目錄,並判斷目錄是否存在
        System.out.println("================建立目錄,並判斷目錄是否存在================");
        File mkdir = new File(path);
        if(mkdir.mkdirs()) {
            System.out.println("目錄 C:/haoxiao/io/ 建立完成");
        } else {
            System.out.println("目錄 C:/haoxiao/io/ 已存在");
        }
        
        //建立檔案
        System.out.println("================建立檔案================");
        File file = new File(path , "Test.txt");
        if(!file.exists()) {
            System.out.println("檔案尚未建立...");
            if(file.createNewFile()) {
                System.out.println("檔案建立成功");
            } else {
                System.out.println("檔案建立失敗");
            }
        } else {
            System.out.println("檔案已經建立");
        }
        
        //更改檔名及路徑
        System.out.println("================更改檔名及路徑================");
        if(!file.exists()) {
            System.out.println("檔案不存在");
        }else{
            System.out.println("檔案更名中...");
            if(!new File(path+"changName.txt").exists()) {
                if(file.renameTo(new File(path + "changName.txt"))) {
                    System.out.println("檔案更名成功");
                } else {
                    System.out.println("檔案更名失敗");
                }
            } else {
                System.out.println("檔案已存在");
            }
        }

        showFile(path);
        
        //刪除檔案
        System.out.println("================刪除檔案================");
        if(file.exists()) {
           file.delete(); 
        }
        showFile(path);
        
        System.out.println("================實做Linux ls指令================");
        path = "C:/windows";
        File file1 = new File(path);
        showDetailFile(file1);
        
    }
    
    public static void showFile(String path) {
        for(String filename : new File(path).list()) {
            System.out.println("Filename : " + filename);
        }
    }
    
    public static void showDetailFile(File file) {
        String fileStrings[] = file.list();
        for(String fileString : fileStrings) {
            File file1 = new File(file.getAbsoluteFile() + File.separator + fileString);
            String isDir = isDirectory(file1);
            String isHidden = isHidden(file1);
            String canRead = canRead(file1);
            String canWrite = canWrite(file1);
            String canExecute = canExecute(file1);
            System.out.println(isDir + "\t" +canRead+canWrite+canExecute + "\t" + isHidden+ fileString + "\t " + file1.lastModified());
        }
    }
    
    public static String canRead(File file) {
        if(file.canRead()) {
            return "r";
        } else {
            return "_";
        }
    }
    
    public static String canWrite(File file) {
        if(file.canWrite()) {
           return "w";
        } else {
            return "_";
        }
    }
    
    public static String canExecute(File file) {
        if(file.canExecute()) {
            return "x";
        } else {
            return "_";
        }
    }
    
    public static String isHidden(File file) {
        if(file.isFile()) {
            return "";
        } else {
            return ".";
        }
    }
    
    public static String isDirectory(File file) {
        if(file.isDirectory()) {
            return "d";
        } else {
            return "_";
        }
    }
}

2008年7月9日 星期三

wine 無法正常使用問題

Q.出現以下 Warning: the specified Windows directory L"c:\\windows" is not accessible. 相關訊息

A.sudo rm -rf ~/.wine after winecfg

原因可能是安裝過程中沒有安裝好,造成 ~/.wine 資料夾中缺少許多設定檔。
將 ~/.wine 全部刪除,在使用winecfg重新設定即可正常使用。