我正在讀取使用java POI的Excel表格中的值,並且需要插入到數據庫中。我從Excel表格中獲取字符串,數字和日期值以及第一個字段是列標題。如何插入數據庫使用java POI從Excel表中讀取數據?
public class SimpleExcelReadExample {
static Connection con1 = null;
static Connection con3 = null;
static PreparedStatement preparedstatement = null;
static ResultSet resultset = null;
int j = 0;
public static void main(String[] args) {
String fileName = "D:/Excel/Report.xls";
Cleartables.table_daily_report();
Vector dataHolder = read(fileName);
saveToDatabase(dataHolder);
}
public static Vector read(String fileName) {
Vector cellVectorHolder = new Vector();
try {
FileInputStream myInput = new FileInputStream(fileName);
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector = new Vector();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
//System.out.println("read method"+myCell);
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
} catch (Exception e) {
e.printStackTrace();
}
return cellVectorHolder;
}
private static void saveToDatabase(Vector dataHolder)
{
for (int i=0;i<dataHolder.size(); i++)
{
Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
for (int j=0; j < cellStoreVector.size();j++)
{
System.out.println("show.....");
HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
Please help me here....How to get the each column values ?
}
對(INT I = 0; I
user441978
但我收到列標題也...但數據庫table.So如何檢索沒有標題的值? – user441978
標題只在第一行,對不對?那麼就跳過那一行? –