81,095
社区成员
发帖
与我相关
我的任务
分享
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.CellType;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Cell;
public class ImportFile {
private Connection connection = null; //定义连接的对象
private PreparedStatement ps = null; //定义预准备的对象
private JDBConnection jdbc = null; //定义数据库连接对象
public boolean readExcel(InputStream fis){
try {
CodeDAO code = new CodeDAO();
GoodsDao goodDao = new GoodsDao();
TbGoods goods = new TbGoods();
jdbc = new JDBConnection();
connection = jdbc.connection;
Workbook wb=Workbook.getWorkbook(fis);
int sheetsNumber=wb.getNumberOfSheets();//得到sheet的个数
for(int a=0;a<sheetsNumber;a++){
Sheet sh=wb.getSheet(a);
int column=sh.getColumns(); //得到总列数
int row=sh.getRows(); //得到总行数
for(int i=1;i<row;i++){ //循环行
for(int j=1;j<column;j++){ //循环列
Cell[] cellRow=sh.getRow(i); //得到当前行的所有单元格
Cell[] cellCol=sh.getColumn(j); //得到当前列的所有单元格
if(cellRow!=null&&cellRow.length>0&&cellCol!=null&&cellCol.length>0){
for(int b=0;b<cellRow.length;b++){ //循环i行里面的单元格
for(int c=1;c<cellCol.length;c++){ //循环i行j列里面的单元格
//取商品代码的值
String spdm=sh.getCell(b, c).getContents().trim();
//取库存的值
String kucun=cellCol[c].getContents().trim();
String sql="update tb_goods set number1="+kucun+" where spdm='"+spdm+"'" ;
ps=connection.prepareStatement(sql);
ps.executeUpdate();
ps.close();
// goods = goodDao.selSpdmGoods(spdm);//根据商品代码查看是否有此代码的商品
// if(goods!=null){
// goods.setNumber1(code.StrToLong(kucun));
// goodDao.updateGoodsInfo(goods);
// }
}
}
}
}
}
}
wb.close();
connection.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}