我需要java代碼來從Excel表格中讀取特定列的數據。 - (lo編號,行號,憑證號,stloc,數量,活動) 這些特定列的值將用於sql查詢(完成jdbc-odbc連接)。 查詢的輸出將與此表中的一列相匹配(此部分將在稍後完成) 請幫助。 sample excel sheet用於讀取特定數據的java程序
0
A
回答
1
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package excelfilereading;
/**
*
* @author vkantiya
*/
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
//
// An excel file name. You can create a file name with a full
// path information.
//
String filename = "FirstExcel.xls";
// Create an ArrayList to store the data read from excel sheet.
//
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
//
// Create a FileInputStream that will be use to read the
// excel file.
//
fis = new FileInputStream(filename);
//
// Create an excel workbook from the file system.
//
HSSFWorkbook workbook = new HSSFWorkbook(fis);
//
// Get the first sheet on the workbook.
//
HSSFSheet sheet = workbook.getSheetAt(0);
//
// When we have a sheet object in hand we can iterator on
// each sheet's rows and on each row's cells. We store the
// data read on an ArrayList so that we can printed the
// content of the excel to the console.
//
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
showExelData(sheetData);
}
private static void showExelData(List sheetData) {
//
// Iterates the data and print it out to the console.
//
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
HSSFCell cell = (HSSFCell) list.get(j);
System.out.print(
cell.getRichStringCellValue().getString());
if (j < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("");
}
}
}
1
查看Apache POI - Microsoft Documents的Java API。
它涵蓋
- 的Excel(SS = HSSF + XSSF)
- 字(HWPF + XWPF)
- 的PowerPoint(HSLF + XSLF)
- OpenXML4J(OOXML)
- OLE2文件系統(POIFS)
- OLE2 Document Props(HPSF)
- Outlook(HSMF)
- 的Visio(HDGF)TNEF(人工鼻)
- 出版商(HPBF)
+0
還有jxl。討論兩個:http://stackoverflow.com/questions/4763624/jexcelapi-vs-apache-poi-which-is-better – heikkim 2012-02-23 14:37:37
相關問題
- 1. 使用驅動程序從特定位置讀取RAM數據?
- 2. 使用Java從DOM讀取特定數據(DOM)
- 3. 快速的方法來讀取特定數據的Java
- 4. 如何讀取特定的行數據
- 5. Python的讀取特定數據表
- 6. 閱讀url數據特定行java
- 7. 經常用於獲取特定數據
- 8. 基於NSDocument的應用程序讀取/寫入SQLite數據庫
- 9. 使用java從特定的端口和指定的IP讀取數據
- 10. 用PHP讀取特定的遠程行
- 11. 從XML文件讀取特定數據
- 12. 延遲從特定數據庫讀取
- 13. 無法從Json讀取特定數據?
- 14. MatLab中特定於應用程序的UDP數據報
- 15. SOAP標頭中特定於應用程序的元數據
- 16. Java InputStream - 如果未使用讀取數據,程序將暫停
- 17. 德爾福和Java應用程序 - 如何讀取數據
- 18. 自定義數據庫會話處理程序 - 特定讀取問題
- 19. 與2個數據庫的軌應用程序 - 1用於讀取其他數據用於寫數據
- 20. 用於Java數據庫應用程序的安裝程序
- 21. 讀取特定行,然後outputing java的
- 22. 從特定行讀取特定數字
- 23. 基於Web的Java應用程序讀取LDAP
- 24. 特定於線程的數據
- 25. Java:從過程中讀取元數據
- 26. ObjectiveC:按特定順序讀取特徵
- 27. 從C#中的Java小程序讀取數據
- 28. 如何讀取數據只有當數值高於或低於特定閾值
- 29. 是否可以使用Java程序從數據透視表中讀取數據?
- 30. Elasticsearch使用Java API抓取所有但僅限於特定數量的數據
您可以使用Apache POI與Excel工作表的工作。使用該API進行編碼並不困難。 – Jayy 2012-02-23 14:33:55