「javajxl使用」Java jxl
今天给各位分享javajxl使用的知识,其中也会对Java jxl进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、有人用过java操作excel表格jxl吗就解答修改时出错
- 2、怎样用java.jxl实现读取excel的数据?求具体代码(以读取3列为例)
- 3、jxl怎样使用自定义颜色设置单元格颜色DevNote
- 4、怎么使用java jxl 提取一个文件夹下多个excel中sheet3中的数据,汇总成一个新的Excel表格
有人用过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(ListUser 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 ListUser readUserExcelFile(File file) throws IOException, BiffException{
if(file==null) return null;
int row,column;
String temp=null;
Workbook book =null;
Sheet sheet=null;
ListUser userList=new ArrayListUser();
book = Workbook.getWorkbook(file);
sheet = book.getSheet(0);
row=3;
while(rowsheet.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的数据?求具体代码(以读取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;irow;i++){
for(int j=0;jcolnum;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里。
jxl怎样使用自定义颜色设置单元格颜色DevNote
xl在Java开源世界中比较有影响力的操作Excel的API工具,使用也很广泛。但是jxl组件中没有提供直接自定义RGB颜色的方法。我们可以通过重置jxl中默认的颜色常量,实现自定义颜色的功能。
代码如下:
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class TestJXLColor {
public static void main(String[] args) {
try {
WritableWorkbook workbook = Workbook.createWorkbook(new File(
"d:\\test-color.xls"));
WritableSheet sheet = workbook.createSheet("测试文字", 0);
// 方法一: 使用jxl默认颜色
WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
10, WritableFont.NO_BOLD);// 字体样式
WritableCellFormat wcf = new WritableCellFormat(font);
wcf.setBackground(Colour.BLUE_GREY);
sheet.addCell(new Label(1, 1, "测试颜色---BLUE_GREY", wcf));
// 方法二:设置自定义颜色,通过java.awt.Color中decode方法提取16进制颜色值
Color color = Color.decode("#EEA9B8"); // 自定义的颜色
workbook.setColourRGB(Colour.ORANGE, color.getRed(),
color.getGreen(), color.getBlue());
WritableCellFormat wcf1 = new WritableCellFormat(font);// 单元格样式
wcf1.setBackground(Colour.ORANGE);
sheet.addCell(new Label(1, 2, "测试颜色---自定义#EEA9B8", wcf1));
// 方法三:设置自定义颜色,按红、绿、蓝的16进制值直接定义颜色值。
workbook.setColourRGB(Colour.LIGHT_BLUE, 0x76, 0xEE, 0x00);
WritableCellFormat wcf2 = new WritableCellFormat(font);// 单元格样式
怎么使用java jxl 提取一个文件夹下多个excel中sheet3中的数据,汇总成一个新的Excel表格
分页查数据,每写完一个sheet,就存一次档(文件保存到硬盘上),并关闭所有文件操作,主动gc;
下一个sheet,重新打开文档,并重复上面一步。
反复重复上面两步,直到你的数据写入完为止。
javajxl使用的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java jxl、javajxl使用的信息别忘了在本站进行查找喔。
发布于:2022-11-28,除非注明,否则均为
原创文章,转载请注明出处。