永发信息网

java程序怎么查询excel表格数据

答案:2  悬赏:30  手机版
解决时间 2021-02-01 16:07
  • 提问者网友:了了无期
  • 2021-02-01 12:06
java程序怎么查询excel表格数据
最佳答案
  • 五星知识达人网友:毛毛
  • 2021-02-01 12:59
前段时间正好做了,导入和导出的,你自己看吧

public void writeExcelBo(FileOutputStream fos, java.util.Vector ve)
{
jxl.write.WritableWorkbook wwb;
try
{
wwb= Workbook.createWorkbook(fos);
jxl.write.WritableSheet ws= wwb.createSheet("booksheet", 10);
ws.addCell(new jxl.write.Label(0, 1, "书目ID"));
ws.addCell(new jxl.write.Label(1, 1, "ISBN"));
ws.addCell(new jxl.write.Label(2, 1, "定价"));
ws.addCell(new jxl.write.Label(3, 1, "书名"));
ws.addCell(new jxl.write.Label(4, 1, "原书名"));
ws.addCell(new jxl.write.Label(5, 1, "副题名"));
ws.addCell(new jxl.write.Label(6, 1, "著者"));
ws.addCell(new jxl.write.Label(7, 1, "译者"));
ws.addCell(new jxl.write.Label(8, 1, "版次"));
ws.addCell(new jxl.write.Label(9, 1, "出版地"));
ws.addCell(new jxl.write.Label(10, 1, "出版社"));
ws.addCell(new jxl.write.Label(11, 1, "出版日期"));
ws.addCell(new jxl.write.Label(12, 1, "页数"));
ws.addCell(new jxl.write.Label(13, 1, "书高"));
ws.addCell(new jxl.write.Label(14, 1, "装帧"));
ws.addCell(new jxl.write.Label(15, 1, "丛书名"));
ws.addCell(new jxl.write.Label(16, 1, "一般性附注项"));
ws.addCell(new jxl.write.Label(17, 1, "简介"));
ws.addCell(new jxl.write.Label(18, 1, "主题词"));
ws.addCell(new jxl.write.Label(19, 1, "中图法分类"));
ws.addCell(new jxl.write.Label(20, 1, "更新日期"));
ws.addCell(new jxl.write.Label(21, 1, "本数"));
book=new Book[ve.size()];
for (int i= 0; i < ve.size(); i++)
{
book[i]= (Book)ve.get(i);
ws.addCell(new jxl.write.Label(0, i + 2, "" + book[i].getBookId()));
ws.addCell(new jxl.write.Label(1, i + 2, book[i].getIsbn()));
ws.addCell(new jxl.write.Label(2, i + 2, "" + book[i].getPrice()));
ws.addCell(new jxl.write.Label(3, i + 2, book[i].getBookTitle()));
ws.addCell(new jxl.write.Label(4, i + 2, book[i].getOldFilename()));
ws.addCell(new jxl.write.Label(5, i + 2, book[i].getSubTitle()));
ws.addCell(new jxl.write.Label(6, i + 2, book[i].getWriter()));
ws.addCell(new jxl.write.Label(7, i + 2, book[i].getTranscribe()));
ws.addCell(new jxl.write.Label(8, i + 2, "" + book[i].getVersion()));
ws.addCell(new jxl.write.Label(9, i + 2, book[i].getPublishCity()));
ws.addCell(new jxl.write.Label(10, i + 2, book[i].getPublisher()));
ws.addCell(new jxl.write.Label(11, i + 2, book[i].getPublishDate().toString()));
ws.addCell(new jxl.write.Label(12, i + 2, "" + book[i].getPage()));
ws.addCell(new jxl.write.Label(13, i + 2, "" + book[i].getHight()));
ws.addCell(new jxl.write.Label(14, i + 2, book[i].getInstall()));
ws.addCell(new jxl.write.Label(15, i + 2, book[i].getSeries()));
ws.addCell(new jxl.write.Label(16, i + 2, book[i].getNotes()));
ws.addCell(new jxl.write.Label(17, i + 2, book[i].getPrecisnotes()));
ws.addCell(new jxl.write.Label(18, i + 2, book[i].getSubject()));
ws.addCell(new jxl.write.Label(19, i + 2, book[i].getCls().replaceAll("_", "")));
ws.addCell(new jxl.write.Label(20, i + 2, book[i].getUpdatedate().toString()));
ws.addCell(new jxl.write.Label(21, i + 2, "0"));
}
jxl.write.WritableFont wfc=
new jxl.write.WritableFont(
WritableFont.ARIAL,
255,
WritableFont.BOLD,
false,
UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfFC= new jxl.write.WritableCellFormat(wfc);
ws.addCell(new jxl.write.Label(0, 0, "为保证您提交定单的稳定和正确,导入定单时候请勿更改此表格式(请勿更改书目ID,订购本数自行添加!)"));
wwb.write();
//关闭Excel工作薄对象
wwb.close();
} catch (IOException e)
{} catch (RowsExceededException e)
{} catch (WriteException e)
{}
}

//导入EXCEL
if (f.getName().indexOf(".xls") > 0)
{
try
{
fis= new FileInputStream(f);
BookBean bob= new BookBean();
UserBean usb= new UserBean();
jxl.Workbook rwb= Workbook.getWorkbook(fis);
jxl.Sheet sh= rwb.getSheet(0);
int rowCount= sh.getRows();
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
book= new Book[rowCount - 1];
for (int i= 1; i < rowCount; i++)
{
book[i - 1]= new Book();
jxl.Cell[] ce= sh.getRow(i);
book[i - 1].setIsbn(ce[0].getContents().toString());
book[i - 1].setSeries(ce[1].getContents().toString());
book[i - 1].setBookTitle(ce[2].getContents().toString());
book[i - 1].setWriter(ce[3].getContents().toString());
book[i - 1].setTranscribe(ce[4].getContents().toString());
book[i - 1].setPublisher(ce[5].getContents().toString());
book[i - 1].setPublishDate(sdf.parse(ce[6].getContents().toString(), new ParsePosition(0)));
book[i-1].setVersion(Integer.parseInt(ce[7].getContents().toString()));
book[i-1].setPage(Integer.parseInt(ce[8].getContents().toString()));
book[i-1].setCls(ce[9].getContents().toString());
book[i-1].setPrecisnotes(ce[10].getContents().toString());
book[i-1].setInstall(ce[11].getContents().toString());
book[i-1].setPrice(Float.parseFloat(ce[12].getContents().toString()));
book[i-1].setUserid(usb.getUser().getUserid());
getVector().addElement(book[i - 1]);
}
rwb.close();
fis.close();
} catch (FileNotFoundException e)
{} catch (BiffException e)
{} catch (IOException e)
{} catch (NumberFormatException e)
{
ShowMessage("数据导入失败,请按照本软件要求的EXCEL格式导入定单");
}
}
全部回答
  • 1楼网友:鸠书
  • 2021-02-01 13:20
前段时间正好做了,导入和导出的,你自己看吧    public void writeexcelbo(fileoutputstream fos, java.util.vector ve)  {   jxl.write.writableworkbook wwb;   try   {    wwb= workbook.createworkbook(fos);    jxl.write.writablesheet ws= wwb.createsheet("booksheet", 10);    ws.addcell(new jxl.write.label(0, 1, "书目id"));    ws.addcell(new jxl.write.label(1, 1, "isbn"));    ws.addcell(new jxl.write.label(2, 1, "定价"));    ws.addcell(new jxl.write.label(3, 1, "书名"));    ws.addcell(new jxl.write.label(4, 1, "原书名"));    ws.addcell(new jxl.write.label(5, 1, "副题名"));    ws.addcell(new jxl.write.label(6, 1, "著者"));    ws.addcell(new jxl.write.label(7, 1, "译者"));    ws.addcell(new jxl.write.label(8, 1, "版次"));    ws.addcell(new jxl.write.label(9, 1, "出版地"));    ws.addcell(new jxl.write.label(10, 1, "出版社"));    ws.addcell(new jxl.write.label(11, 1, "出版日期"));    ws.addcell(new jxl.write.label(12, 1, "页数"));    ws.addcell(new jxl.write.label(13, 1, "书高"));    ws.addcell(new jxl.write.label(14, 1, "装帧"));    ws.addcell(new jxl.write.label(15, 1, "丛书名"));    ws.addcell(new jxl.write.label(16, 1, "一般性附注项"));    ws.addcell(new jxl.write.label(17, 1, "简介"));    ws.addcell(new jxl.write.label(18, 1, "主题词"));    ws.addcell(new jxl.write.label(19, 1, "中图法分类"));    ws.addcell(new jxl.write.label(20, 1, "更新日期"));    ws.addcell(new jxl.write.label(21, 1, "本数"));    book=new book[ve.size()];    for (int i= 0; i < ve.size(); i++)    {     book[i]= (book)ve.get(i);     ws.addcell(new jxl.write.label(0, i + 2, "" + book[i].getbookid()));     ws.addcell(new jxl.write.label(1, i + 2, book[i].getisbn()));     ws.addcell(new jxl.write.label(2, i + 2, "" + book[i].getprice()));     ws.addcell(new jxl.write.label(3, i + 2, book[i].getbooktitle()));     ws.addcell(new jxl.write.label(4, i + 2, book[i].getoldfilename()));     ws.addcell(new jxl.write.label(5, i + 2, book[i].getsubtitle()));     ws.addcell(new jxl.write.label(6, i + 2, book[i].getwriter()));     ws.addcell(new jxl.write.label(7, i + 2, book[i].gettranscribe()));     ws.addcell(new jxl.write.label(8, i + 2, "" + book[i].getversion()));     ws.addcell(new jxl.write.label(9, i + 2, book[i].getpublishcity()));     ws.addcell(new jxl.write.label(10, i + 2, book[i].getpublisher()));     ws.addcell(new jxl.write.label(11, i + 2, book[i].getpublishdate().tostring()));     ws.addcell(new jxl.write.label(12, i + 2, "" + book[i].getpage()));     ws.addcell(new jxl.write.label(13, i + 2, "" + book[i].gethight()));     ws.addcell(new jxl.write.label(14, i + 2, book[i].getinstall()));     ws.addcell(new jxl.write.label(15, i + 2, book[i].getseries()));     ws.addcell(new jxl.write.label(16, i + 2, book[i].getnotes()));     ws.addcell(new jxl.write.label(17, i + 2, book[i].getprecisnotes()));     ws.addcell(new jxl.write.label(18, i + 2, book[i].getsubject()));     ws.addcell(new jxl.write.label(19, i + 2, book[i].getcls().replaceall("_", "")));     ws.addcell(new jxl.write.label(20, i + 2, book[i].getupdatedate().tostring()));     ws.addcell(new jxl.write.label(21, i + 2, "0"));    }    jxl.write.writablefont wfc=     new jxl.write.writablefont(      writablefont.arial,      255,      writablefont.bold,      false,      underlinestyle.no_underline,      jxl.format.colour.black);    jxl.write.writablecellformat wcffc= new jxl.write.writablecellformat(wfc);    ws.addcell(new jxl.write.label(0, 0, "为保证您提交定单的稳定和正确,导入定单时候请勿更改此表格式(请勿更改书目id,订购本数自行添加!)"));    wwb.write();    //关闭excel工作薄对象    wwb.close();   } catch (ioexception e)   {} catch (rowsexceededexception e)   {} catch (writeexception e)   {}  }    //导入excel    if (f.getname().indexof(".xls") > 0)    {     try     {      fis= new fileinputstream(f);      bookbean bob= new bookbean();      userbean usb= new userbean();      jxl.workbook rwb= workbook.getworkbook(fis);      jxl.sheet sh= rwb.getsheet(0);      int rowcount= sh.getrows();      simpledateformat sdf= new simpledateformat("dd/mm/yyyy");      book= new book[rowcount - 1];      for (int i= 1; i < rowcount; i++)      {       book[i - 1]= new book();       jxl.cell[] ce= sh.getrow(i);       book[i - 1].setisbn(ce[0].getcontents().tostring());       book[i - 1].setseries(ce[1].getcontents().tostring());       book[i - 1].setbooktitle(ce[2].getcontents().tostring());       book[i - 1].setwriter(ce[3].getcontents().tostring());       book[i - 1].settranscribe(ce[4].getcontents().tostring());       book[i - 1].setpublisher(ce[5].getcontents().tostring());       book[i - 1].setpublishdate(sdf.parse(ce[6].getcontents().tostring(), new parseposition(0)));       book[i-1].setversion(integer.parseint(ce[7].getcontents().tostring()));       book[i-1].setpage(integer.parseint(ce[8].getcontents().tostring()));       book[i-1].setcls(ce[9].getcontents().tostring());       book[i-1].setprecisnotes(ce[10].getcontents().tostring());       book[i-1].setinstall(ce[11].getcontents().tostring());       book[i-1].setprice(float.parsefloat(ce[12].getcontents().tostring()));       book[i-1].setuserid(usb.getuser().getuserid());       getvector().addelement(book[i - 1]);      }      rwb.close();      fis.close();     } catch (filenotfoundexception e)     {} catch (biffexception e)     {} catch (ioexception e)     {} catch (numberformatexception e)     {      showmessage("数据导入失败,请按照本软件要求的excel格式导入定单");     }    }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯