import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import com.tl.config.Constants;
import com.tl.po.commom.ExcelInfo;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class SimpleExcelWrite {
/**
* java生成Excel工作薄文件至本地目录
* @param fileName 文件名
* @param h 行
* @return
* @throws Exception
*/
public static void exportExcel(String fileName,List list,List addressees){
try {
String filePath = Constants.DISK + fileName +".xls";
// 首先创建一个用于保存excel表格的文件
File file = new File(filePath);
// 创建一个文件输出流,用于写出表格到本地文件夹
OutputStream out = new FileOutputStream(file);
// 创建一个可读写入的工作薄(相当于创建了一个excel表格。抽象类,不能用new来创建)
WritableWorkbook workbook = Workbook.createWorkbook(out);
// 创建一个新页(一个excel文件可以包含多页哦。参数一:本页名称;参数二:页数,第一页为0)
WritableSheet sheet = workbook.createSheet("患者信息", 0);
// 添加单元格至本页中(目前表格最左上角应该有一个“步骤一”的单元格)
//制作表头
String[] heads = new String[]{"日期","床号","姓名","性别","年龄","诊断","病历号","穿刺部位","穿刺静脉选择","穿刺体位","导管尖端位置","术侧上臂壁围","原导管长度","置入长度","体外留管"};
Label cell = new Label(0, 0, heads[0]); // 1行1列
sheet.addCell(cell);
for (int j = 0; j < heads.length; j++) {
cell = new Label(j, 0, heads[j]); // 1行2列
sheet.addCell(cell);
}
int i=1;
for (ExcelInfo excelInfo : list) {
cell = new Label(0, i, excelInfo.getDate());
sheet.addCell(cell);
cell = new Label(1, i, excelInfo.getBedNo());
sheet.addCell(cell);
cell = new Label(2, i, excelInfo.getName());
sheet.addCell(cell);
cell = new Label(3, i, excelInfo.getSex());
sheet.addCell(cell);
cell = new Label(4, i, excelInfo.getAge());
sheet.addCell(cell);
cell = new Label(5, i, excelInfo.getDisease());
sheet.addCell(cell);
cell = new Label(6, i, excelInfo.getBlNo());
sheet.addCell(cell);
cell = new Label(7, i, excelInfo.getCcbw());
sheet.addCell(cell);
cell = new Label(8, i, excelInfo.getCcjm());
sheet.addCell(cell);
cell = new Label(9, i, excelInfo.getCctw());
sheet.addCell(cell);
cell = new Label(10, i, excelInfo.getDgjd());
sheet.addCell(cell);
cell = new Label(11, i, excelInfo.getScsb());
sheet.addCell(cell);
cell = new Label(12, i, excelInfo.getDgcd());
sheet.addCell(cell);
cell = new Label(13, i, excelInfo.getZrcd());
sheet.addCell(cell);
cell = new Label(14, i, excelInfo.getTwlg());
sheet.addCell(cell);
i++;
}
workbook.write(); // 写入Excel工作表
workbook.close(); // 关闭Excel工作表,同时也会关闭IO流,勿忘。
if(!addressees.isEmpty()){
SendEmail.sendEmail(filePath,addressees);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}