多平台统一管理软件接口,如何实现多平台统一管理软件接口
282
2023-05-10
java简单解析xls文件的方法示例【读取和写入】
本文实例讲述了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 for (int j = 0; 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.Dathttp://eTime 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程序设计有所帮助。
for (int j = 0; 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.Dathttp://eTime 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程序设计有所帮助。
//注意,这里的两个参数,第一个是表示列的,第二才表示行
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.Dathttp://eTime 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程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~