通过jxl.jar 读取、导出excel的实例代码
package export.excel;
import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.outputstream;
import java.util.arraylist;
import java.util.list;
import java.util.random;
import jxl.cell;
import jxl.sheet;
import jxl.workbook;
import jxl.format.alignment;
import jxl.format.verticalalignment;
import jxl.write.label;
import jxl.write.writablecellformat;
import jxl.write.writablesheet;
import jxl.write.writableworkbook;
import export.vo.personvo;
/**导出excel文件
*
* @author 路人甲
*
*/
public class exportexcel {
public list<personvo> personvolist;
public exportexcel()
{
initdata();
}
/**
* 初始化数据
*/
public void initdata()
{
// 随机函数
random random = new random();
personvolist = new arraylist<personvo>();
// 初始化100个人进集合
personvo personvo = null;
for (int i = 0; i < 100; i++)
{
personvo = new personvo();
personvo.setid("" + i);
personvo.setname("路人" + i);
if (i%2==0)
{
personvo.setsex("男");
}
else
{
personvo.setsex("女");
}
// 取100岁以内的随机年龄
personvo.setage("" + random.nextint(100));
personvolist.add(personvo);
}
}
/**导出数据以excel格式导出
* exportname 导出文件名称
* @param exportname
*/
public void exportperson(string exportname)
{
try{
file excelfile = new file(exportname + "temp.xls");
// 文件格式
writablecellformat format = new writablecellformat();
// x 靠左
format.setalignment(alignment.right);
// y 靠顶
format.setverticalalignment(verticalalignment.top);
// 创建一个工作文件
writableworkbook writableworkbook = workbook.createworkbook(excelfile);
// 创建一个工作簿
writablesheet sheet1 = writableworkbook.createsheet("个人信息列表", 0);
//创建行数 设置行的宽度
sheet1.setcolumnview(0, 10);
sheet1.setcolumnview(1, 10);
sheet1.setcolumnview(2, 10);
sheet1.setcolumnview(3, 10);
// 设置行的值
sheet1.addcell(new label(0, 0, "id", format));
sheet1.addcell(new label(1, 0, "姓名", format));
sheet1.addcell(new label(2, 0, "性别", format));
sheet1.addcell(new label(3, 0, "年龄", format));
// 把集合写入到excel中
int rownum = 1;
for (personvo bean : personvolist)
{
int colspannum = 0;
sheet1.addcell(new label(colspannum, rownum, bean.getid(), format));
sheet1.addcell(new label(colspannum++, rownum, bean.getname(), format));
sheet1.addcell(new label(colspannum++, rownum, bean.getsex(), format));
sheet1.addcell(new label(colspannum++, rownum, bean.getage(), format));
rownum++;
}
writableworkbook.write();
writableworkbook.close();
// 如果是在网络下载的,那么就写这些
// super.gethttpservletresponse().setcontenttype("application/x-msdownload");
// string encodetittle = new string(excelname.getbytes("gbk"), "iso-8859-1");
// super.gethttpservletresponse().addheader("content-disposition","attachment;filename="+encodetittle+".xls");
fileinputstream finput = new fileinputstream(excelfile);
// outputstream output = super.gethttpservletresponse().getoutputstream();
file fout = new file(exportname + ".xls");
outputstream output = new fileoutputstream(fout);
bufferedinputstream buffin = new bufferedinputstream(finput);
bufferedoutputstream buffout = new bufferedoutputstream(output);
byte[] buffer = new byte[4096];
int count = 0;
while ((count = buffin.read(buffer, 0, buffer.length)) > 0) {
buffout.write(buffer, 0, count);
}
buffin.close();
buffout.close();
finput.close();
output.close();
excelfile.delete();
}
catch (exception e) {
e.printstacktrace();
}
finally
{
system.out.println("完成导出操作");
}
}
/**导入数据以excel格式导出
*
* @param importexcel 导入excel文件名称
*/
public void importperson(string importexcel)
{
try{
file excelfile = new file(importexcel+".xls");
// 创建一个工作文件
workbook workbook = workbook.getworkbook(excelfile);
// 获得第一个工作簿 这里有两种方法获取sheet表,1为名字,而为下标,从0开始
sheet sheet = workbook.getsheet(0);
// 总记录数
int allrow = sheet.getrows();
int allcolspan = sheet.getcolumns();
system.out.println(allrow);
system.out.println(allcolspan);
// 取数据
for (int i=0; i<allrow; i++)
{
// 取出每一列的值
for (int j=0; j<allcolspan; j++)
{
// sheet.getcell(列数, 行数);
cell cell = sheet.getcell(j, i);
// 打印出该列的值
system.out.print(cell.getcontents() + "\t");
}
system.out.println();
}
//关闭
workbook.close();
}catch (exception e) {
e.printstacktrace();
}
finally
{
system.out.println("完成导入操作");
}
}
/**
* @param args
*/
public static void main(string[] args) {
exportexcel exportexcel = new exportexcel();
// 设置路径
string srcpath = "c:/quarantine/personvo";
// 导出
// exportexcel.exportperson(srcpath);
// 读取
exportexcel.importperson(srcpath);
}
}
上一篇: 什么是超文本传输协议(java与javascript的区别)
下一篇: 希尔排序的算法代码
推荐阅读