查询以获取excel中的工作表名称(java)

时间:2013-12-26 09:56:43

标签: java database excel

我想获取excel文件中存在的工作表名称列表。是否有任何查询来获取工作表名称。我正在使用
                   的Class.forName( “com.hxtt.sql.excel.ExcelDriver”)的newInstance();

1 个答案:

答案 0 :(得分:0)

您可以使用Apache的POI库。 More help here on how to do it with example

或者,您可以使用JXL库并使用以下代码:

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.read.biff.BiffException;

public class GetSheetNamesTest {

  /**
   * @param args
   * @throws IOException 
   * @throws BiffException 
   */
  public static void main(String[] args) throws BiffException, IOException {
    //Read the given XL sheet 
    Workbook workbook = Workbook.getWorkbook(new File("C:/JXL/Sheet-Names.xls"));
    System.out.println("Number of sheets in this workbook : " + workbook.getNumberOfSheets());

    String [] sheetNames = workbook.getSheetNames();

    for (int i = 0 ; i < sheetNames.length ; i ++ ) {
      System.out.println("Sheet Name[" + i + "] = " + sheetNames[i]);
    }

    //Close and free allocated memory 
    workbook.close(); 
  }

}