java 读excel è¿æ¯æ¯è¾æ¹ä¾¿ç®åçï¼åçå°±æ¯ï¼å
ç¨java 读åexcelï¼ç¶åï¼ä¸è¡è¡çåå
¥æ°æ®åºï¼å段çè¯ï¼ä½ èªå·±ç¨åºéé¢åå°±è¡äºï¼ç»ä½ 个ä¾åï¼
ä»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.*;
/**
* Created by IntelliJ IDEA.
* User: xl
* Date: 2005-7-17
* Time: 9:33:22
* To change this template use File | Settings | File Templates.
*/
public class ExcelHandle
{
public ExcelHandle()
{
}
/**
* 读åExcel
*
* @param filePath
*/
public static void readExcel(String filePath)
{
try
{
InputStream is = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(is);
//Sheet st = rwb.getSheet("0")è¿éæ两ç§æ¹æ³è·åsheet表,1为ååï¼è为ä¸æ ï¼ä»0å¼å§
Sheet st = rwb.getSheet("original");
Cell c00 = st.getCell(0,0);
//éç¨çè·åcellå¼çæ¹å¼,è¿åå符串
String strc00 = c00.getContents();
//è·å¾cellå
·ä½ç±»åå¼çæ¹å¼
if(c00.getType() == CellType.LABEL)
{
LabelCell labelc00 = (LabelCell)c00;
strc00 = labelc00.getString();
}
//è¾åº
System.out.println(strc00);
//å
³é
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* è¾åºExcel
*
* @param os
*/
public static void writeExcel(OutputStream os)
{
try
{
/**
* åªè½éè¿APIæä¾çå·¥åæ¹æ³æ¥å建Workbookï¼èä¸è½ä½¿ç¨WritableWorkbookçæé å½æ°ï¼
* å 为类WritableWorkbookçæé å½æ°ä¸ºprotectedç±»å
* method(1)ç´æ¥ä»ç®æ æ件ä¸è¯»åWritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));
* method(2)å¦ä¸å®ä¾æ示 å°WritableWorkbookç´æ¥åå
¥å°è¾åºæµ
*/
WritableWorkbook wwb = Workbook.createWorkbook(os);
//å建Excelå·¥ä½è¡¨ æå®å称åä½ç½®
WritableSheet ws = wwb.createSheet("Test Sheet 1",0);
//**************å¾å·¥ä½è¡¨ä¸æ·»å æ°æ®*****************
//1.æ·»å Label对象
Label label = new Label(0,0,"this is a label test");
ws.addCell(label);
//æ·»å 带æååFormatting对象
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);
//æ·»å 带æåä½é¢è²çFormatting对象
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);
//2.æ·»å Number对象
Number labelN = new Number(0,1,3.1415926);
ws.addCell(labelN);
//æ·»å 带æformattingçNumber对象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
ws.addCell(labelNF);
//3.æ·»å Boolean对象
Boolean labelB = new jxl.write.Boolean(0,2,false);
ws.addCell(labelB);
//4.æ·»å DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
ws.addCell(labelDT);
//æ·»å 带æformattingçDateFormat对象
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);
//æ·»å å¾ç对象,jxlåªæ¯æpngæ ¼å¼å¾ç
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);//copy
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
{
//读Excel
ExcelHandle.readExcel("f:/testRead.xls");
//è¾åºExcel
File fileWrite = new File("f:/testWrite.xls");
fileWrite.createNewFile();
OutputStream os = new FileOutputStream(fileWrite);
ExcelHandle.writeExcel(os);
//ä¿®æ¹Excel
ExcelHandle.modifyExcel(new file(""),new File(""));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
2.å¨jspä¸åç¸å
³æµè¯ï¼å建ä¸ä¸ªwriteExcel.jsp
<%
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"%>ä¸è¡ã
温馨提示:内容为网友见解,仅供参考