2个回答
展开全部
// 找到导入的文件
String fileToBeRead= "exl的路径及文件名";
String cell_value=new String();
int iRowCount,iColCount; //得到Excel表的行数,列数
try{
// 创建对Excel工作簿文件的
FileInputStream fs_xls=new FileInputStream(fileToBeRead);
POIFSFileSystem fs_pio = new POIFSFileSystem(fs_xls);
HSSFWorkbook workbook = new HSSFWorkbook(fs_pio);
// 创建对工作表的。
// 本例是按名(让我们假定那张表有着缺省名 "Sheet1 ")
//HSSFSheet sheet = workbook.getSheetAt( "Sheet1 ");
// 也可用getSheetAt(int index)按索引,
// 在Excel文档中,第一张工作表的缺省索引是0,
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
//读取exl的第一张表
HSSFSheet sheet = workbook.getSheetAt(0);
iRowCount = sheet.getPhysicalNumberOfRows();
iColCount = sheet.getRow(0).getPhysicalNumberOfCells();
out.println("Excel表行数和列数:"+iRowCount+","+iColCount+ "<br>");
//开始从第二行读取这个Excel表
for(int i=2;i<iRowCount;i++){
//创建空的变量接受exl的值
String xid = null;
String xxname = null;
HSSFRow row = sheet.getRow(i);
if(sheet.getRow(i)!=null){
for(short j=0;j<iColCount;j++){
HSSFCell cell =row.getCell(j);
if (cell!=null){
switch(cell.getCellType()) //判断类型
{
case HSSFCell.CELL_TYPE_NUMERIC:
String newD = ((Double)cell.getNumericCellValue()).toString() ;
cell_value = newD.substring(0,newD.indexOf("."));
break;
case HSSFCell.CELL_TYPE_STRING:
cell_value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
cell_value = cell.getCellFormula();
break;
default:
//cell_value= " <font color=red> null_value </font> ";
cell_value = "f";
} //end switch
//out.print(cell_value+ " ");
if(!"f".equals(cell_value)){
if(xid==null)
{
xid = new String((cell_value).getBytes("utf-8"), "iso-8859-1");
//xid = cell_value;
continue;
}
if(xxname==null)
{
xxname = new String((cell_value).getBytes("utf-8"), "iso-8859-1");
//xxname =cell_value;
}
String sql="insert into stu1(xid,xxname) values('"+xid+"','"+xxname+"')";
System.out.println("sql==================="+sql);
//执行sql插入
} //end if(!"f".equals(cell_value)){
cell_value= " ";
} //end if
} //end for (short j = 0; j < iColCount; j++) {
//out.print( " <br> ");
} //end if(sheet.getRow(i)==null){
else
iRowCount++; //由于sheet.getPhysicalNumberOfRows()方法不统计空行,所以遇到空行时,要使此值加1才是实际行数
} //end for (int i = 0; i < iRowCount; i++) {
fs_xls.close(); //关闭文件流
}catch(Exception e){
System.out.println( "出错了 : " + e );
}
大致就是这样的,我用的是poi-2.5.1.jar,或者poi-3.0.1-FINAL-20070705.jar,具体哪个也忘了,你可以都导入,可能有些问题,你先看着吧
String fileToBeRead= "exl的路径及文件名";
String cell_value=new String();
int iRowCount,iColCount; //得到Excel表的行数,列数
try{
// 创建对Excel工作簿文件的
FileInputStream fs_xls=new FileInputStream(fileToBeRead);
POIFSFileSystem fs_pio = new POIFSFileSystem(fs_xls);
HSSFWorkbook workbook = new HSSFWorkbook(fs_pio);
// 创建对工作表的。
// 本例是按名(让我们假定那张表有着缺省名 "Sheet1 ")
//HSSFSheet sheet = workbook.getSheetAt( "Sheet1 ");
// 也可用getSheetAt(int index)按索引,
// 在Excel文档中,第一张工作表的缺省索引是0,
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
//读取exl的第一张表
HSSFSheet sheet = workbook.getSheetAt(0);
iRowCount = sheet.getPhysicalNumberOfRows();
iColCount = sheet.getRow(0).getPhysicalNumberOfCells();
out.println("Excel表行数和列数:"+iRowCount+","+iColCount+ "<br>");
//开始从第二行读取这个Excel表
for(int i=2;i<iRowCount;i++){
//创建空的变量接受exl的值
String xid = null;
String xxname = null;
HSSFRow row = sheet.getRow(i);
if(sheet.getRow(i)!=null){
for(short j=0;j<iColCount;j++){
HSSFCell cell =row.getCell(j);
if (cell!=null){
switch(cell.getCellType()) //判断类型
{
case HSSFCell.CELL_TYPE_NUMERIC:
String newD = ((Double)cell.getNumericCellValue()).toString() ;
cell_value = newD.substring(0,newD.indexOf("."));
break;
case HSSFCell.CELL_TYPE_STRING:
cell_value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
cell_value = cell.getCellFormula();
break;
default:
//cell_value= " <font color=red> null_value </font> ";
cell_value = "f";
} //end switch
//out.print(cell_value+ " ");
if(!"f".equals(cell_value)){
if(xid==null)
{
xid = new String((cell_value).getBytes("utf-8"), "iso-8859-1");
//xid = cell_value;
continue;
}
if(xxname==null)
{
xxname = new String((cell_value).getBytes("utf-8"), "iso-8859-1");
//xxname =cell_value;
}
String sql="insert into stu1(xid,xxname) values('"+xid+"','"+xxname+"')";
System.out.println("sql==================="+sql);
//执行sql插入
} //end if(!"f".equals(cell_value)){
cell_value= " ";
} //end if
} //end for (short j = 0; j < iColCount; j++) {
//out.print( " <br> ");
} //end if(sheet.getRow(i)==null){
else
iRowCount++; //由于sheet.getPhysicalNumberOfRows()方法不统计空行,所以遇到空行时,要使此值加1才是实际行数
} //end for (int i = 0; i < iRowCount; i++) {
fs_xls.close(); //关闭文件流
}catch(Exception e){
System.out.println( "出错了 : " + e );
}
大致就是这样的,我用的是poi-2.5.1.jar,或者poi-3.0.1-FINAL-20070705.jar,具体哪个也忘了,你可以都导入,可能有些问题,你先看着吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询