java如何读写本地已有的excel? 楼主: 我就是要读老的excel表。再更改,再保存。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java写啊?觉得还是使用js在客户端做好些!就我知道的,现在很多用activeX方式实现! 读:在Java中读取Excel文件的内容 在这里,我使用的是一个叫Java Excel API的东西,类似的还有jakarta的POI,不过感觉那个太复杂了点儿。而且jxl对中文的支持相当的好,至少我在用的过程中一点问题没出。 二、特性可以读取Excel 95, 97, 2000文件;可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug);生成Excel 97格式的电子表格、支持字体、数字和日期格式化、支持单元格的颜色和阴影、可以编辑现有的文件 三、读文件//声明一下,记得后面要关闭哦。。Workbook workbook = null; try { workbook = Workbook.getWorkbook(new File("d:\\temp\\TestRead.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(); 写:在Java中向Excel文件写入内容 四、导出数据到Excel文件中下面的例子,设置了数字、日期的格式,还有字体,颜色等。 File tempFile=new File("d:/temp/output.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 Titleint 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 detailint 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(); 请问楼上:System.out.print(((NumberCell)cell).getValue());这是往后台写吧??我要读出到我的jsp页面上呢??---然后在页面上修改,再保存。 我所实现的是,把库里的数据检索出来之之后,保存到excel里,即在jsp页面上显示excel表.在这里可以修改和保存excel数据.至于,把已经存在的excel表调用,即读到jsp页面,我还没做过.我是参照我给你贴的帖子是实现了我上面说的功能.我只是希望给你一点启发或者拓宽思路.实际上还是自己动手做. java web中怎么判断当前服务是用的weblogic还是tomcat? 二级联动 修改 保持二级数据,欢迎讨论 急急急.页面传值,求助各位大侠! ssh项目报的错 购物网站求助。。。。。 在mysql中约束 菜鸟问题 请教安全方面的资料!!做论文用,请讨论:) 无人回帖,如何结贴? 如何点链接进行文件下载? 如何在jsp页面中调用sql server的备份功能? 有这样的函数嘛?
在Java中读取Excel文件的内容
在这里,我使用的是一个叫Java Excel API的东西,类似的还有jakarta的POI,不过感觉那个
太复杂了点儿。而且jxl对中文的支持相当的好,至少我在用的过程中一点问题没出。
二、特性
可以读取Excel 95, 97, 2000文件;可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug);生成Excel 97格式的电子表格、支持字体、数字和日期格式化、支持单元格的颜色和阴影、可以编辑现有的文件 三、读文件
//声明一下,记得后面要关闭哦。。
Workbook workbook = null; try {
workbook = Workbook.getWorkbook(new File("d:\\temp\\TestRead.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();
写:
在Java中向Excel文件写入内容
四、导出数据到Excel文件中
下面的例子,设置了数字、日期的格式,还有字体,颜色等。 File tempFile=new File("d:/temp/output.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();
System.out.print(((NumberCell)cell).getValue());
这是往后台写吧??
我要读出到我的jsp页面上呢??
---然后在页面上修改,再保存。