JXL导出excel Output_Excl
程序员文章站
2024-03-20 22:15:34
...
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Excel {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 设置excel工作表的将要显示的列标题
String[] title = { "项目编号", "项目名称", "项目成本(万元)", "项目收益", "结项日期",
"项目实施地点", "是否外包" };
try {
//根据时间给生成的EXCEL文件命名
SimpleDateFormat sdf_hms = new SimpleDateFormat("yyyyMMddHHmmss");
String filename = sdf_hms.format(new Date());
String filePath = "c:\\" + filename + ".xls";
// 创建Excel工作薄
WritableWorkbook wwb;
OutputStream os = new FileOutputStream(filePath);
wwb = Workbook.createWorkbook(os);
// 添加第一个工作表并设置第一个Sheet的名字
WritableSheet sheet = wwb.createSheet("2009年项目清单列表", 0);
/*
* 通过web.getSheet(0)获得第一个sheet
* CellFormat cf = wwb.getSheet(0).getCell(1, 0).getCellFormat();
* */
Label label;
//将列标题循环添加到Label中
for (int i = 0; i < title.length; i++) {
label = new Label(i, 0, title[i]);
sheet.addCell(label);
}
// 下面是填充数据
/*
* 保存数字到单元格,需要使用jxl.write.Number
* 必须使用其完整路径,否则会出现错误
* */
// 填充项目编号
jxl.write.Number number = new jxl.write.Number(0, 1, 20090113);
sheet.addCell(number);
label = new Label(1, 1, "某公司财务系统");
sheet.addCell(label);
/*
* 定义对于显示金额的公共格式
* jxl会自动实现四舍五入
* 例如 12.456会被格式化为12.46,12.454会被格式化为12.45
* */
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat(
nf);
jxl.write.Number nb = new jxl.write.Number(2, 1, 12.45, wcf);
sheet.addCell(nb);
// 填充产品数量
jxl.write.Number numb = new jxl.write.Number(3, 1, 20000);
sheet.addCell(numb);
/* 填充出产日期
* 定义显示日期的公共格式
* 如:yyyy-MM-dd hh:mm
* */
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String newdate = sdf.format(new Date());
label = new Label(4, 1, newdate);
sheet.addCell(label);
label = new Label(5, 1, "陕西西安");
sheet.addCell(label);
/*
* 显示布尔值
* */
jxl.write.Boolean bool = new jxl.write.Boolean(6, 1, true);
sheet.addCell(bool);
WritableCellFormat wc = new WritableCellFormat();
// 设置居中
wc.setAlignment(Alignment.CENTRE);
// 设置边框线
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
// 设置单元格的背景颜色
wc.setBackground(jxl.format.Colour.YELLOW);
/*
* 合并单元格
* 通过writablesheet.mergeCells(int x,int y,int m,int n);来实现的
* 表示将从第x+1列,y+1行到m+1列,n+1行合并
*
* */
wwb.write();
wwb.close();
} catch (Exception e) {
System.out.println("---出现异常---");
e.printStackTrace();
}
}
public void exportExcel(List list, String fileName,
HttpServletResponse response) {
try {
//list为要打印的数据,fileName为要打印文件的名称
response.setContentType("application/x-msdownload");
//获得当前系统时间
String time = (new Timestamp(System.currentTimeMillis()))
.toString().substring(2, 10);
//获得导出文件的名称
String dispposition = "=?UTF-8?Q?attachment; fileName="
+ URLEncoder.encode(fileName, "UTF-8") + time + ".xls";
response.setHeader("Content-Disposition", dispposition);//设置文件的名称
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(os);//WritableWorkbook可写的Excel工作簿
WritableSheet ws = wwb.createSheet(fileName, 0);//WritableSheet工作簿内的可写工作表
//WritableFont可以设定字体信息
//第一个参数代表字体类型,第二个代表字体大小,第三个代表是否粗体,第四个代表是否斜体,第五个代表是否需要下划线,第六个代表字体的颜色
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK); //获得第一行标题样式
WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);//获得除第一行的所有数据样式
//WritableCellFormat设定单元格的格式化信息
WritableCellFormat wcfF = new WritableCellFormat();
wcfF = new WritableCellFormat(wf);
WritableCellFormat wcfF1 = new WritableCellFormat();
wcfF1 = new WritableCellFormat(wf1);
//设置行,列的宽度
List minList = (List) list.get(0);
for (int nB = 0; nB < minList.size(); nB++) {
String max = "";
for (int nI = 0; nI < list.size(); nI++) {
//Object minList1 = (Object) list.get(nI);
//String value = String.valueOf(minList1.get(nB).toString());
String value = String
.valueOf(((List) list.get(nI)).get(nB));
if (!value.equals("null")) {
if (max.length() < value.length()) {
max = value;
}
}
}
//int countLength = max.length();
byte[] countLength = max.getBytes();
ws.setColumnView(nB, countLength.length + 5);//设置行的宽度
}
for (int nB = 0; nB < list.size(); nB++) {
ws.setRowView(nB, 300);//设置行的高度
}
int countField = minList.size();//获得list中的字段数
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < countField; j++) {
//Label内容为字符类型的Cell,可读可写
Label labelC;
String value = String.valueOf(((List) list.get(i)).get(j));
if (i == 0) {
if (value.equals("null")) {
labelC = new Label(j, i, "", wcfF);
} else {
labelC = new Label(j, i, ((List) list.get(i))
.get(j).toString().trim(), wcfF);
}
} else {
if (value.equals("null")) {
labelC = new Label(j, i, "", wcfF1);
} else {
labelC = new Label(j, i, ((List) list.get(i))
.get(j).toString().trim(), wcfF1);
}
}
//第一个是代表列数,第二是代表行数,第三个代表要写入的内容,第四个是可选项,是输入这个label里面的样式
ws.addCell(labelC);
}
}
wwb.write();
//关闭Excel工作薄对象
wwb.close();
os.close();
} catch (Exception e) {
//System.out.println("失败");
}
}
}
上一篇: java 把数据导出导出excl
下一篇: 微服务架构