java简单解析xls文件的方法示例【读取和写入】
程序员文章站
2023-12-19 22:37:46
本文实例讲述了java简单解析xls文件的方法。分享给大家供大家参考,具体如下:
读取:
import java.io.*;
import jxl.*;
i...
本文实例讲述了java简单解析xls文件的方法。分享给大家供大家参考,具体如下:
读取:
import java.io.*; import jxl.*; import jxl.write.*; import jxl.format.*; class aa{ public static void main(string args[]) { try{ workbook workbook = null; try { workbook = workbook.getworkbook(new file("d:\\a.xls")); } catch (exception e) { throw new exception("file to import not found!"); } sheet sheet = workbook.getsheet(0); cell cell = null; int columncount=3; int rowcount=sheet.getrows(); for (int i = 0; i <rowcount; i++) { for (int j = 0; j <columncount; j++) { //注意,这里的两个参数,第一个是表示列的,第二才表示行 cell=sheet.getcell(j, i); //要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确 if(cell.gettype()==celltype.number){ system.out.print(((numbercell)cell).getvalue()); } else if(cell.gettype()==celltype.date){ system.out.print(((datecell)cell).getdate()); } else{ system.out.print(cell.getcontents()); } //system.out.print(cell.getcontents()); system.out.print("\t"); } system.out.print("\n"); } //关闭它,否则会有内存泄露 workbook.close(); }catch(exception e){ } } }
写入:
import java.io.*; import jxl.*; import jxl.write.*; import jxl.format.*; class aa{ public static void main(string args[]) { try{ file tempfile=new file("d:" + java.io.file.separator + "output00.xls"); system.out.println( "d:" + java.io.file.separator + "output00.xls" ); writableworkbook workbook = workbook.createworkbook(tempfile); writablesheet sheet = workbook.createsheet("testcreateexcel", 0); //一些临时变量,用于写到excel中 label l=null; jxl.write.number n=null; jxl.write.datetime d=null; //预定义的一些字体和格式,同一个excel中最好不要有太多格式 writablefont headerfont = new writablefont(writablefont.arial, 12, writablefont.bold, false, underlinestyle.no_underline, jxl.format.colour.blue); writablecellformat headerformat = new writablecellformat (headerfont); writablefont titlefont = new writablefont(writablefont.arial, 10, writablefont.no_bold, false, underlinestyle.no_underline, jxl.format.colour.red); writablecellformat titleformat = new writablecellformat (titlefont); writablefont detfont = new writablefont(writablefont.arial, 10, writablefont.no_bold, false, underlinestyle.no_underline, jxl.format.colour.black); writablecellformat detformat = new writablecellformat (detfont); numberformat nf=new numberformat("0.00000"); //用于number的格式 writablecellformat priceformat = new writablecellformat (detfont, nf); dateformat df=new dateformat("yyyy-mm-dd");//用于日期的 writablecellformat dateformat = new writablecellformat (detfont, df); //剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中 l=new label(0, 0, "用于测试的excel文件", headerformat); sheet.addcell(l); //add title int column=0; l=new label(column++, 2, "标题", titleformat); sheet.addcell(l); l=new label(column++, 2, "日期", titleformat); sheet.addcell(l); l=new label(column++, 2, "货币", titleformat); sheet.addcell(l); l=new label(column++, 2, "价格", titleformat); sheet.addcell(l); //add detail int i=0; column=0; l=new label(column++, i+3, "标题 "+i, detformat); sheet.addcell(l); d=new datetime(column++, i+3, new java.util.date(), dateformat); sheet.addcell(d); l=new label(column++, i+3, "cny", detformat); sheet.addcell(l); n=new jxl.write.number(column++, i+3, 5.678, priceformat); sheet.addcell(n); i++; column=0; l=new label(column++, i+3, "标题 "+i, detformat); sheet.addcell(l); d=new datetime(column++, i+3, new java.util.date(), dateformat); sheet.addcell(d); l=new label(column++, i+3, "sgd", detformat); sheet.addcell(l); n=new jxl.write.number(column++, i+3, 98832, priceformat); sheet.addcell(n); //设置列的宽度 column=0; sheet.setcolumnview(column++, 20); sheet.setcolumnview(column++, 20); sheet.setcolumnview(column++, 10); sheet.setcolumnview(column++, 20); workbook.write(); workbook.close(); }catch(exception e){ } } }
更多关于java相关内容感兴趣的读者可查看本站专题:《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。