1. 笔记:
OutputStream os ; Label label; File file = new File(srcFilePath); File[] list=file.listFiles(); os=new FileOutputStream(dstDir+"pnrdata_a330.xls"); WritableWorkbook wwb = Workbook.createWorkbook(os);//创建xls文件 WritableSheet ws = wwb.createSheet("PNRDATA",0);//设置sheet名 //写抬头
label = new Label(0,0,"PNR_ID");
ws.addCell(label); label = new Label(1,0,"File_Name"); ws.addCell(label); label = new Label(2,0,"Start_Index"); ws.addCell(label); label = new Label(3,0,"End_Index"); ws.addCell(label);
wwb.write();
2.首先需要一个JXL包,下载地址:http://download.csdn.net/source/292830 (1)生成EXCEL需要手动写查询语句把ORACLE数据库中的数据查询出来,再通过操作写到EXCEL文件里面。 (2)通过EXCEL把数据读取到ORACLE,同样需要去读取EXCEL工作薄里面的内容,再通过INSERT语句去插入数据库操作。
示例: 包括从Excel读取数据,生成新的Excel,以及修改Excel
- package common.util;
-
- import jxl.*;
- import jxl.format.UnderlineStyle;
- import jxl.write.*;
- import jxl.write.Number;
- import jxl.write.Boolean;
-
- import java.io.*;
-
-
- public class ExcelHandle
- {
- public ExcelHandle()
- {
- }
-
-
-
-
-
-
- public static void readExcel(String filePath)
- {
- try
- {
- InputStream is = new FileInputStream(filePath);
- Workbook rwb = Workbook.getWorkbook(is);
-
- Sheet st = rwb.getSheet("original");
- Cell c00 = st.getCell(0,0);
-
- String strc00 = c00.getContents();
-
- if(c00.getType() == CellType.LABEL)
- {
- LabelCell labelc00 = (LabelCell)c00;
- strc00 = labelc00.getString();
- }
-
- System.out.println(strc00);
-
- rwb.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
- public static void writeExcel(OutputStream os)
- {
- try
- {
-
-
-
-
-
-
-
- WritableWorkbook wwb = Workbook.createWorkbook(os);
-
- WritableSheet ws = wwb.createSheet("Test Sheet 1",0);
-
-
-
-
- Label label = new Label(0,0,"this is a label test");
- ws.addCell(label);
-
-
- WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
- WritableCellFormat wcf = new WritableCellFormat(wf);
- Label labelcf = new Label(1,0,"this is a label test",wcf);
- ws.addCell(labelcf);
-
-
- WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
- UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
- WritableCellFormat wcfFC = new WritableCellFormat(wfc);
- Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);
- ws.addCell(labelCF);
-
-
- Number labelN = new Number(0,1,3.1415926);
- ws.addCell(labelN);
-
-
- NumberFormat nf = new NumberFormat("#.##");
- WritableCellFormat wcfN = new WritableCellFormat(nf);
- Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
- ws.addCell(labelNF);
-
-
- Boolean labelB = new jxl.write.Boolean(0,2,false);
- ws.addCell(labelB);
-
-
- jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
- ws.addCell(labelDT);
-
-
- DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
- WritableCellFormat wcfDF = new WritableCellFormat(df);
- DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);
- ws.addCell(labelDTF);
-
-
-
- File image = new File("f:\\2.png");
- WritableImage wimage = new WritableImage(0,1,2,2,image);
- ws.addImage(wimage);
-
- wwb.write();
- wwb.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
-
- /**
- * 拷贝后,进行修改,其中file1为被copy对象,file2为修改后创建的对象
- * 尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去,
- * 以使单元格的内容以不同的形式表现
- * @param file1
- * @param file2
- */
- public static void modifyExcel(File file1,File file2)
- {
- try
- {
- Workbook rwb = Workbook.getWorkbook(file1);
- WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);
- WritableSheet ws = wwb.getSheet(0);
- WritableCell wc = ws.getWritableCell(0,0);
-
- if(wc.getType == CellType.LABEL)
- {
- Label label = (Label)wc;
- label.setString("The value has been modified");
- }
- wwb.write();
- wwb.close();
- rwb.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
-
-
-
- public static void main(String[] args)
- {
- try
- {
-
- ExcelHandle.readExcel("f:/testRead.xls");
-
- File fileWrite = new File("f:/testWrite.xls");
- fileWrite.createNewFile();
- OutputStream os = new FileOutputStream(fileWrite);
- ExcelHandle.writeExcel(os);
-
- ExcelHandle.modifyExcel(new file(""),new File(""));
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
2.在jsp中做相关测试,创建一个writeExcel.jsp
- <%
- response.reset();
- response.setContentType("application/vnd.ms-excel");
- File fileWrite = new File("f:/testWrite.xls");
- fileWrite.createNewFile();
- new FileOutputStream(fileWrite);
- ExcelHandle.writeExcel(new FileOutputStream(fileWrite));
- %>
- 在IE中浏览writeExcel.jsp就可以动态生成Excel文档了,其中response.setContentType("application/vnd.ms-excel");语句必须要,才能确保不乱码,在jsp中输入<%@page contentType="application/vnd.ms-excel;charset=GBK"%>不行。
<%
response.reset();//清除Buffer
response.setContentType("application/vnd.ms-excel");
File fileWrite = new File("f:/testWrite.xls");
fileWrite.createNewFile();
new FileOutputStream(fileWrite);
ExcelHandle.writeExcel(new FileOutputStream(fileWrite));
%>
在IE中浏览writeExcel.jsp就可以动态生成Excel文档了,其中response.setContentType("application/vnd.ms-excel");语句必须要,才能确保不乱码,在jsp中输入<%@page contentType="application/vnd.ms-excel;charset=GBK"%>不行。
|