`
Java_大猫
  • 浏览: 169840 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

jxl导入excel

    博客分类:
  • J2SE
阅读更多
昨天做一个 excel 导入。由于此部分用的是JXL 所以 记录下来。分享下。
我这里需要把SQL 写入到txt中。但是和导入excel一样。


	
	public static Sheet getSheet(String realPath) throws Exception{
		  InputStream is = new FileInputStream(realPath);
		  Workbook rwb = Workbook.getWorkbook(is);
		  Sheet rs = rwb.getSheet(0);
		  return rs;
	}


public static void insertSQL() throws Exception{
		Sheet rs = getSheetDEMO();
		int rows = rs.getRows();
		AA cpdc = new AA();
		BBcid = new BBcid ();
		FileWriter   fw=new   FileWriter( "E:\\API\\demo.txt",true);
		BufferedWriter   bw=new   BufferedWriter(fw);  
	     
		
		for(int i=1; i<rows;i++){
			cid.setShortpn("".equals(rs.getCell(0, i).getContents().trim()) ? "" : rs.getCell(0, i).getContents().trim());
			cid.setHscode("".equals(rs.getCell(1, i).getContents().trim()) ? "" : rs.getCell(1, i).getContents().trim());
			cpdc.setLongname("".equals(rs.getCell(2, i).getContents().trim()) ? "" : rs.getCell(2, i).getContents().trim());
			cpdc.setShortname("".equals(rs.getCell(3, i).getContents().trim()) ? "" : rs.getCell(3, i).getContents().trim());
			cpdc.setNamecn("".equals(rs.getCell(4, i).getContents().trim()) ? "" : rs.getCell(4, i).getContents().trim());
	
String sql="INSERT INTO A_TABLE (SHORTPN, HSCODE, LONGNAME,SHORTNAME, NAMECN) " +
		"VALUES ('"+cid.getShortpn()+"', '"+cid.getHscode()+"', '"+cpdc.getLongname()+"'," +
				" '"+cpdc.getShortname()+"', '"+cpdc.getNamecn()+"');\r\n";
		bw.write(sql);
		}
		bw.close();
		fw.close(); 
	}
	
	public static void main(String[] args) throws Exception {
		insertSQL();
		System.out.println("导出结束");
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics