javaexceljxl
⑴ 有人用過java操作excel表格jxl嗎就解答修改時出錯
Java Excel 是一個開源項目,通過它Java開發人員可以讀取Excel文件的內容、創建新的Excel文件、更新已經存在的Excel文件等,在項目中需要導入名為jxl.jar的包。在這里只是示例它的基本用法,其他高級的功能(圖片、公式、格式等)請參考Java Excel的幫助文檔。
如有一個用戶資料的Excel表,包含ID、用戶名、性別、郵件等信息,定義一個用戶JavaBean:
package com.monitor1394.excel;
/**
*
* 用戶
*
* @author monitor
* Created on 2010-12-22, 9:57:58
*/
public class User {
/** ID */
private int id;
/** 用戶名 */
private String name;
/** 性別 1:男 2:女*/
private int sex;
/** 郵件 */
private String email;
public User(){
}
public User(int id,String name,int sex,String email){
this.id=id;
this.name=name;
this.sex=sex;
this.email=email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
@Override
public String toString(){
return id+":"+name;
}
}
提供的Excel表操作類如下,某些單元格的格式可按自己意願指定:
package com.monitor1394.excel;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
*
* Excel表操作
*
* @author monitor
* Created on 2010-12-22, 9:50:28
*/
public class Excel {
/** 標題單元格格式 */
private static WritableCellFormat titleFormat=null;
/** 主題內容單元格格式 */
private static WritableCellFormat bodyFormat=null;
/** 注釋單元格格式 */
private static WritableCellFormat noteFormat=null;
/** 浮點型數據的單元格格式 */
private static WritableCellFormat floatFormat=null;
/** 整型數據的單元格格式 */
private static WritableCellFormat intFormat=null;
/** 初始化數據 */
private static boolean init=false;
/** 私有構造方法,防止錯誤使用Excel類 */
private Excel(){
}
/**
* 初始化各單元格格式
* @throws WriteException 初始化失敗
*/
private static void init() throws WriteException{
WritableFont font1,font2,font3,font4;
//Arial字體,9號,粗體,單元格黃色,田字邊框,居中對齊
font1 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD, false);
titleFormat = new WritableCellFormat (font1);
titleFormat.setBackground(Colour.YELLOW);
titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
titleFormat.setAlignment(Alignment.CENTRE);
//Arial字體,9號,粗體,單元格黃色,田字邊框,左右居中對齊,垂直居中對齊,自動換行
font2 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD, false);
noteFormat = new WritableCellFormat (font2);
noteFormat.setBackground(Colour.YELLOW);
noteFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
noteFormat.setAlignment(Alignment.CENTRE);
noteFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
noteFormat.setWrap(true);
//Arial字體,9號,非粗體,單元格淡綠色,田字邊框
font3 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false);
bodyFormat = new WritableCellFormat (font3);
bodyFormat.setBackground(Colour.LIGHT_GREEN);
bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
//Arial字體,9號,非粗體,單元格淡綠色,田字邊框
font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false);
floatFormat = new WritableCellFormat (font4,NumberFormats.FLOAT);
floatFormat.setBackground(Colour.LIGHT_GREEN);
floatFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
//Arial字體,9號,非粗體,單元格淡綠色,田字邊框
font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false);
intFormat = new WritableCellFormat (font4,NumberFormats.INTEGER);
intFormat.setBackground(Colour.LIGHT_GREEN);
intFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
init=true;
}
public static void createUserExcelFile(List<User> userList,File destFile) throws WriteException, IOException{
if(init==false) init();
int index,row;
WritableSheet sheet=null;
WritableWorkbook book=null;
book = Workbook.createWorkbook(destFile);
sheet = book.createSheet("用戶表", 0);
sheet.setColumnView(0, 15);
sheet.setColumnView(1, 15);
sheet.setColumnView(2, 15);
sheet.setColumnView(3, 40);
//欄位變數名
index=0;
sheet.addCell(new Label(index++,0,"id",titleFormat));
sheet.addCell(new Label(index++,0,"name",titleFormat));
sheet.addCell(new Label(index++,0,"sex",titleFormat));
sheet.addCell(new Label(index++,0,"email",titleFormat));
//欄位名
index=0;
sheet.addCell(new Label(index++,1,"ID",titleFormat));
sheet.addCell(new Label(index++,1,"用戶名",titleFormat));
sheet.addCell(new Label(index++,1,"性別",titleFormat));
sheet.addCell(new Label(index++,1,"郵件",titleFormat));
//欄位注釋
index=0;
sheet.addCell(new Label(index++,2,null,noteFormat));
sheet.addCell(new Label(index++,2,null,noteFormat));
sheet.addCell(new Label(index++,2,"1:男/n2:女",noteFormat));
sheet.addCell(new Label(index++,2,null,noteFormat));
row=3;
for(User user:userList){
if(user==null) continue;
index=0;
sheet.addCell(new Number(index++,row,user.getId(),bodyFormat));
sheet.addCell(new Label(index++,row,user.getName(),bodyFormat));
sheet.addCell(new Number(index++,row,user.getSex(),bodyFormat));
sheet.addCell(new Label(index++,row,user.getEmail(),bodyFormat));
row++;
}
book.write();
if(book!=null) book.close();
}
public static List<User> readUserExcelFile(File file) throws IOException, BiffException{
if(file==null) return null;
int row,column;
String temp=null;
Workbook book =null;
Sheet sheet=null;
List<User> userList=new ArrayList<User>();
book = Workbook.getWorkbook(file);
sheet = book.getSheet(0);
row=3;
while(row<sheet.getRows()){
column=0;
User user=new User();
//id
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("") && temp.matches("//d+")) user.setId(Integer.parseInt(temp));
else break;
//名稱
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("")) user.setName(temp);
//性別
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("") && temp.matches("//d+")) user.setSex(Integer.parseInt(temp));
//郵件
temp=sheet.getCell(column++,row).getContents().trim();
if(temp!=null && !temp.equals("")) user.setEmail(temp);
userList.add(user);
row++;
}
if(book!=null) book.close();
return userList;
}
}
要導入的Excel表格式如下:
導出後的Excel表如下:
⑵ java,jxl,對excel的操作,現在已經有一個xls文件,其中有內容,怎樣在讀取它的同時向其中插入一列類容
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(newpath));
HSSFSheet sheet = workbook.getSheetAt(0); //獲取工作表
HSSFCell cell = sheet.getRow(j).getCell((short) i);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(value); //往單元格放值
i與j分別代表所在的列與行,
⑶ java excel導入用jxl好還是poi好
個人感覺POI好用一些,因為在銀行工作,開發的很多報表功能都要導出,導入excel數據,用的都是POI。至於POI使用的教程可以上網找找,或者到阿帕奇官網查看API文檔:http://poi.apache.org/apidocs/index.html
⑷ java用jxl操作excel時,excel表格線如何顯示
添加內容的時候不要把模板中的表格對象覆蓋掉,最好把你的代碼發上來。
⑸ java jxl操作excel
暫沒有找到用jxl另存的辦法。
想問一下,你的excel如果不另存直接保存,可以不可以用POI打開?
⑹ Java 連接EXCEL問題(jxl)
Workbook info=Workbook.getWorkbook(new File("e:/jdk/er/Award.xls"));
這個語句,有可能導致IOException,所以必須把它放在try-catch語句中。
try{
//.......
}
catch(IOException ioe)
{
//如果在拋出IOException時,你想進行一些處理,就將語句寫在這里
}
⑺ java用jxl包對EXCEL進行操作的問題
我這有過這樣一個程序。
也是畫好的Excel模板。
先讀取這個模板,生成一個Excel對象。關閉輸入流。
然後往Excel對象裡面寫數據。
最後輸出到某個路徑。
⑻ 用java jxl讀取excel 編輯保存為新的excel文件「±」 正負號編譯錯誤怎麼辦
你文件流輸出的時候設定下utf-8格式,± 出現的單元格 值類型設定為string
⑼ 怎樣用java.jxl實現讀取excel的數據求具體代碼(以讀取3列為例)
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class MyExcel {
public static void main(String[] args) {
try {
Workbook workbook = Workbook.getWorkbook(new File("C:\\myfile.xls"));
Sheet sheet = workbook.getSheet(0);//使用第一個工作表
int colnum = sheet.getColumns();//獲取列數,如果一定要3列,直接改3就行
int row = sheet.getRows();//獲取行數
StringBuffer sb = new StringBuffer();
for(int i=0;i<row;i++){
for(int j=0;j<colnum;j++){
Cell c = sheet.getCell(j,i);//獲得單元數據
sb.append(c.getContents()+"\n");
}
}
workbook.close();
System.out.println(sb);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這個寫法有很多種,這里只是給您參考。要讀取內容關鍵是以下幾步:
1.創建一個可讀取的excel文件對象
Workbook workbook = Workbook.getWorkbook(new File("C:\\myfile.xls"));//注意文件路徑
2.獲取工作表
Sheet sheet = workbook.getSheet(0);//使用第一個工作表
3.獲取單元格數據,我的例子里是通過循環獲取所有的數據
sheet.getCell(j,i);
4.最後把獲取的數據做你所需要的處理。
sb.append(c.getContents()+"\n");//我這里把它加到了StringBuffer里。
⑽ java 使用jxl操作excel,向excel中插入對象
應該是不能 對象這個東西Excel根本就沒有 你咋加到他上邊
就和想把Excel的公式導到MyEclipse裡面一樣