百度搜索圈T社區(qū)免費行業(yè)視頻教程
www.aiquanti.com
packagecom.asima;
importjava.io.FileInputStream;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
*@authorasima
*@data2006-10-18
*/
publicclassXlsToAccess
{
HSSFSheetglobalSheet=null;
/*讀取一個指定單元格內(nèi)容*/
publicStringreadCellValue(Stringpos)
{
intxpos;
shortypos;
intcellType;/*取得此單元格的類型0-Numeric,1-String,3-null*/
Stringresult;/*返回取得的單元格的值*/
ypos=(short)(pos.toUpperCase().charAt(0)-65);
xpos=Integer.parseInt(pos.substring(1,pos.length()))-1;
HSSFRowrow=null;/*定義excel中的行*/
HSSFCellcell=null;/*定義excel中的單元格*/
/*根據(jù)xPos和yPos取得單元格*/
row=globalSheet.getRow(xpos);
cell=row.getCell(ypos);
/****************此處如果是空需要修改***********************************/
cellType=cell.getCellType();
switch(cellType)
{
case0:/*0-Numeric*/
result=String.valueOf(cell.getNumericCellValue());
break;
case1:/*1-String*/
result=cell.getStringCellValue();
break;
case3:/*3-null*/
result="";
break;
default:
result="";
break;
}
returnresult;
}
/*讀取excel文件并把內(nèi)容插入到access表中*/
publicvoidinsertIntoTable()throwsException
{
//創(chuàng)建對Excel工作簿文件的引用
HSSFWorkbookworkbook=
newHSSFWorkbook(newFileInputStream("D:/temp/test.xls"));
//獲得一個sheet
globalSheet=workbook.getSheetAt(0);
Stringvalue1=readCellValue("c1");
Stringvalue2=readCellValue("c2");
Stringvalue3=readCellValue("c3");
Stringvalue4=readCellValue("c4");
System.out.println(value1);
System.out.println(value2);
/*插入數(shù)據(jù)庫*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Stringurl="jdbc:odbc:asima";
Connectionconn=DriverManager.getConnection(url);
PreparedStatementstmt=
conn.prepareStatement("insertintocustomvalues(?,?,?,?)");
//定義查詢的SQL語句
stmt.setString(1,value1);
stmt.setString(2,value2);
stmt.setString(3,value3);
stmt.setString(4,value4);
stmt.executeUpdate