用java代码读取excel文档插入SQL Server数据库,好像有个JXL.jar包是吧,具体如何有知道的大侠告知告知,谢谢!!
------解决方案--------------------
读过CSV
BUT
http://blog.csdn.net/manytao/article/details/5819853 这个也许能帮你的忙。
------解决方案--------------------
给你个例子吧
private void importYdjksj(HttpServletRequest req, HttpServletResponse resp){
String status="0";
StringBuffer msg_err=new StringBuffer();
StringBuffer msg=new StringBuffer();
try {
YdjksjDao dao = new YdjksjDao();
String fileName = null; //导入文件名
int rowNum=0; //导入文件记录数
int rowNum_pre=3; //从前开始需要减的行数
int rowNum_last=1; //从后开始需要减的行数
int rowNum_start=4; //开始行数索引
int errNum=0;
String filePath="E:/tmp/import";
String filePath_new="";
filePath_new=generateFile(req, filePath, fileName);
Workbook wb = null;
try{
File file = new File(filePath_new);//(this.getHttpServletRequest().getParameter("sjwj"));
wb=Workbook.getWorkbook(file);
} catch (BiffException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(wb==null)
throw new Exception("文件对象为空!");
try {
Sheet[] sheet = wb.getSheets();
if(sheet!=null&&sheet.length>0){
rowNum = sheet[0].getRows();
String jkyh="";
String yhzh="";
String bsrq="";
jkyh=getSpeciBySplit(sheet[0].getRow(1), "dsyh:");
yhzh=getSpeciBySplit(sheet[0].getRow(1), "ylshdm:");
bsrq=getSpeciBySplit(sheet[0].getRow(2), "bbfsrq:");
System.out.println("jkyh:"+jkyh+", yhzh:"+yhzh+", bsrq:"+bsrq);
if (rowNum > rowNum_pre){
//删除当天原有记录
dao.deleteBySql("delete WFJKSJ_YD t where to_char(t.gxsj,'yyyy-MM-dd')=to_char(sysdate,'yyyy-MM-dd')", null);
//导入记录
for(int j=rowNum_start;j<rowNum-rowNum_last;j++){
try{
YdjksjValueObject vo=new YdjksjValueObject();
Cell[] cells = sheet[0].getRow(j);
if(cells!=null&&cells.length>0){
vo.setUnitname(cells[0].getContents());
vo.setUnitno(cells[1].getContents());
vo.setPnno(cells[2].getContents());
vo.setFkje(cells[3].getContents());
vo.setZnj(cells[4].getContents());
vo.setHtje(cells[5].getContents());
vo.setPjlx(cells[6].getContents());
vo.setPjhm(cells[7].getContents());
vo.setJksj(cells[8].getContents());
System.out.println("j:"+j+"pnno:"+vo.getPnno()+", len:"+vo.getPnno().length());
vo.setJdslb("1");
vo.setZt("0");
vo.setJkyh(jkyh);
vo.setYhzh(yhzh);
vo.setBsrq(bsrq);
vo.setOrderindex(Integer.toString(j));
String msg_tmp="";
msg_tmp=dao.saveYdjksj(vo);
if(msg_tmp!=null && !"".equals(msg_tmp)){
throw new Exception(msg_tmp);