您的位置:首页 > 移动开发 > Android开发

android jxl对excel进行读、写、修改操作,设置excel列宽

2017-02-28 14:25 621 查看
1、首先下载 jxl开发包 jxl.jar 点击下载

2.excel工具类

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class WriteExcel {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

private static File hasFile;

/**
* 同步操作,防止并发。
*
* @param args
* @return
* @throws IOException
* @throws RowsExceededException
* @throws WriteException
*/
public synchronized static String[] write(String[] args)
throws IOException, RowsExceededException, WriteException {

// 文件路径
// 判断文件是否存在,如果存在就不创建,追加,如果不存在则创建文件并追加。
WritableWorkbook book = Workbook.createWorkbook(getHasFile());
book.setProtected(true);
// -- 第一个参数是Sheet名,第二个参数是Sheet下标
// -- 下标是整数,只起标识作用,建立的时候会以create顺序建立,本例生成的EXCEL文件第一个Sheet是sheet1
WritableSheet sheet = book.createSheet("第一页", 1);
sheet.setColumnView(0, 20);//设置excel表格中对应列宽 (x,width) x表示列  y表示宽度
sheet.setColumnView(1, 20);
sheet.setColumnView(2, 5);
sheet.setColumnView(3, 20);
sheet.setColumnView(4, 20);
sheet.setColumnView(5, 20);
sheet.setColumnView(6, 20);
sheet.setColumnView(7, 20);
sheet.setColumnView(8, 20);
//设置自适应宽度的方法,这里对中文字段无效,标题必须为英文才行
//CellView cellView = new CellView();
//cellView.setAutosize(true); //设置自动大小
//sheet.setColumnView(0, cellView);

sheet.getSettings().setProtected(true);
sheet.getSettings().setPassword("xxxx");//设置密码
String[] title = { "支付宝交易号", "订单号", "交易总金额", "商品名称/订单名称", "商品描述/订单备注",
"买家支付宝账号", "交易状态", "sign", "交易时间" };
for (int i = 0; i < title.length; i++) {
Label lable = new Label(i, 0, title[i]);
sheet.addCell(lable);
}
// 初次创建,写入一行。
for (int i = 0; i < title.length; i++) {
Label lable = new Label(i, 1, args[i]);
sheet.addCell(lable);
}
// 每次写入数据时,写到最后一行。
book.write();
book.close();
System.out.println("写入成功");
return null;
}

/**
* 追加excel
*
* @param args
* @throws IOException
* @throws BiffException
* @throws WriteException
* @throws RowsExceededException
*/
public static void addExcel(File file, String[] args) throws BiffException,
IOException, RowsExceededException, WriteException {
Workbook book = Workbook.getWorkbook(file);
Sheet sheet = book.getSheet(0);
// 获取行
int length = sheet.getRows();
System.out.println(length);
WritableWorkbook wbook = Workbook.createWorkbook(file, book); // 根据book创建一个操作对象
WritableSheet sh = wbook.getSheet(0);// 得到一个工作对象
// 从最后一行开始加
for (int i = 0; i < args.length; i++) {
Label label = new Label(i, length, args[i]);
sh.addCell(label);
}
wbook.write();
wbook.close();
}
/**
*  File file ecxel文件
*  int lie  第几列
*  int rows 第几行
*  String content 需要改变的内容
**/
public static void updateExcel(File excelFile,int lie,int rows,String content){
// new File("D://test.xls")
try {
//获得Excel文件
Workbook rw= Workbook.getWorkbook(excelFile);
//打开一个文件副本,并且指定数据写回到原文件
WritableWorkbook book = Workbook.createWorkbook(excelFile, rw);// 这里两处file需一直,表明写回原文件中

//得到对应的单元格(列,行)
WritableCell writeCL = sheet.getWritableCell(lie, rows);
if(writeCL.getType() == CellType.LABEL){
Label l = (Label)writeCL;
l.setString(content);//对单元格里面的内容进行设置
}
book.write();
book.close();
rw.close();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* 判断文件是否已经写入
*
* @param filename
* @return
*/
public static boolean filecheck(String filename) {
boolean flag = false;
File file = new File(filename);
if (file.exists()) {
flag = true;
}
setHasFile(file);
return flag;
}

/**
* 不管神马类型,都转换成string
*
* @param obj
* @return
*/
public static String converToString(Object obj) {
return "";
}

public static void main(String[] args) throws RowsExceededException,
WriteException, IOException, BiffException {
String filepath = WriteExcel.class.getResource("/").getPath()
+ sdf.format(new Date()) + ".xls";
String[] str = { "20101020102032032", "2012203203232032032", "50",
"100元朗识币", "这个订单没有备注", "1234566@163.com", "STATU_SUCCESS",
"ssdhfksdhfksdjhfkshdsdlfd", sdf.format(new Date()) };
boolean has = WriteExcel.filecheck(filepath);
// 如果存在
if (has)
addExcel(getHasFile(), str);
else {
write(str);
}

}

/**
* @return the hasFile
*/
public static File getHasFile() {
return hasFile;
}

/**
* @param hasFile
*            the hasFile to set
*/
public static void setHasFile(File hasFile) {
WriteExcel.hasFile = hasFile;
}

}


执行第一次,在common/classes下创建了一个yyyyMMd.xls的文件,写入2行数据。

再次执行,此文件数据追加一行。

可用来操作数据量不大的数据保存。

方便下载查看以及储存。

缺点:追加数据的时候读取原来的文件作为副本然后新建一个工作对象,数据量大会导致内存溢出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel jxl