关于网友提出的“ 高分求从excel导入数据到数据库的demo”问题疑问,本网通过在网上对“ 高分求从excel导入数据到数据库的demo”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 高分求从excel导入数据到数据库的demo
描述: 如题,这2天研究这个头要爆炸了,有没有demo学习下,SSH框架的 万分感激,表头有中文,有日期类型
解决方案1: jxl的解析,解析后的结果你可以设计一个类,专门存放,用于插入数据库
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;<>
public class ReadExcel {
public Workbook rwbook = null;
public Sheet sheet = null;
public InputStream in = null;
public void LoadExcel(String fileName) throws IOException {
try {
in = new FileInputStream(fileName);
rwbook = Workbook.getWorkbook(in);
this.getOneSheet(0); // 报文中只有一个sheet,所以默认取第一个。
} catch (FileNotFoundException e) {
/>
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
in.close();
}
}
/**
* 获取第X张Sheet表
*
* @param x
* int
*/
public void getOneSheet(int x) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
sheet = rwbook.getSheet(x);
int countRows = sheet.getRows();
int countCol = sheet.getColumns();
Cell c;
for (int i=0;i<>
for(int j = 0 ;j<>
c = sheet.getCell(j,i);
//日期类型转换
if (c.getType() == CellType.DATE)
{
DateCell dateCell=(DateCell)c;
System.out.print(sf.format(dateCell.getDate())+" ");
continue;
}
System.out.print(c.getContents()+" ");
}
System.out.println();
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
/>
// TODO Auto-generated method stub
ReadExcel re = new ReadExcel();
re.LoadExcel("D:\\xx.xls");
}
}
以上介绍了“ 高分求从excel导入数据到数据库的demo”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2273092.html