标签:
@SuppressWarnings("deprecation")
public void exportStudent(){
String fileName="考场信息表"+DateKit.getCurrentDateTime("yyyyMMddhhmmss")+".xls";
String path=getRequest().getRealPath("\\")+"upload\\excel\\"+fileName;
/*File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
String desktopPath = desktopDir.getAbsolutePath();*/
System.out.println(path);
File file=new File(path);
HSSFWorkbook wb = new HSSFWorkbook(); //--->创建了一个excel文件
HSSFSheet sheet = wb.createSheet("考场信息表"); //--->创建了一个工作簿
HSSFDataFormat format= wb.createDataFormat(); //--->单元格内容格式
sheet.setColumnWidth((short)3, 20* 256); //---》设置单元格宽度,因为一个单元格宽度定了那么下面多有的单元格高度都确定了所以这个方法是sheet的
sheet.setColumnWidth((short)4, 40* 256); //--->第一个参数是指哪个单元格,第二个参数是单元格的宽度
sheet.setDefaultRowHeight((short)150); // ---->有得时候你想设置统一单元格的高度,就用这个方法
sheet.setColumnWidth((short)0, 20* 256);
sheet.setColumnWidth((short)1, 30* 256);
sheet.setColumnWidth((short)2, 40* 256);
sheet.setColumnWidth((short)5, 20* 256);
sheet.setColumnWidth((short)6, 20* 256);
sheet.setColumnWidth((short)7, 20* 256);
sheet.setColumnWidth((short)8, 20* 256);
sheet.setColumnWidth((short)9, 20* 256);
sheet.setColumnWidth((short)10, 20* 256);
sheet.setColumnWidth((short)11, 20* 256);
sheet.setColumnWidth((short)12, 20* 256);
sheet.setColumnWidth((short)13, 20* 256);
//样式1
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
//设置标题字体格式
HSSFFont font = wb.createFont();
//设置字体样式
font.setFontHeightInPoints((short)12); //--->设置字体大小
font.setFontName("Courier New"); //---》设置字体,是什么类型例如:宋体
font.setItalic(false); //--->设置是否是加粗
style.setFont(font); //--->将字体格式加入到style中
//style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
// style.setFillPattern("YELLOW");//设置单元格颜色
style.setWrapText(false); //设置是否能够换行,能够换行为true
//表格第三行
HSSFRow row4 = sheet.createRow(0);
row4.setHeightInPoints((short)25);
HSSFCell cell4_1 = row4.createCell((short)0);
cell4_1.setCellStyle(style);
cell4_1.setCellValue("场次");
HSSFCell cell4_2 = row4.createCell((short)1);
cell4_2.setCellStyle(style);
cell4_2.setCellValue("地市");
HSSFCell cell4_3 = row4.createCell((short)2);
cell4_3.setCellStyle(style);
cell4_3.setCellValue("学校");
HSSFCell cell4_4 = row4.createCell((short)3);
cell4_4.setCellStyle(style);
cell4_4.setCellValue("学生姓名");
HSSFCell cell4_5 = row4.createCell((short)4);
cell4_5.setCellStyle(style);
cell4_5.setCellValue("公位号");
HSSFCell cell4_6 = row4.createCell((short)5);
cell4_6.setCellStyle(style);
cell4_6.setCellValue("代号");
try{
Long competitionId = getParaToLong("competitionId");
Qb qb = new Qb("select a.xsId,a.code,a.userCode,b.xm,c.name,d.city,d.xxmc FROM CttParticipant a left join CttXS b on a.xsId = b.id left join CttRonda c on a.rondaId=c.id left join CttXX d on b.xxId=d.id WHERE a.competitionId=? ",competitionId);
if (StrKit.isNotEmpty(getPara("XsName"))) {
qb.append(" and b.xm like ?", Qb.wrapLike(getPara("XsName")));
}
if (StrKit.isNotEmpty(getPara("city"))) {
qb.append(" and d.city like ?", Qb.wrapLike(getPara("city")));
}
if (StrKit.isNotEmpty(getPara("xxName"))) {
qb.append(" and d.xxmc like ?", Qb.wrapLike(getPara("xxName")));
}
if (StrKit.isNotEmpty(getPara("userCode"))) {
qb.append(" and a.userCode like ?", Qb.wrapLike(getPara("userCode")));
}
qb.append(getOrderBy("c.name,a.code"));
List<Record> list = Db.find(qb);
int j = 0; //增加行
for (Record record : list) {
HSSFRow rowN = sheet.createRow(1+j);
rowN.setHeightInPoints((short)25);
HSSFCell cell_1 = rowN.createCell((short)0);
cell_1.setCellStyle(style);
if(StrKit.isNotEmpty(record.getStr("name"))){
cell_1.setCellValue(record.getStr("name"));
}else{
cell_1.setCellValue("");
}
HSSFCell cell_2 = rowN.createCell((short)1);
cell_2.setCellStyle(style);
if(StrKit.isNotEmpty(record.getStr("city"))){
cell_2.setCellValue(record.getStr("city"));
}else{
cell_2.setCellValue("");
}
HSSFCell cell_3 = rowN.createCell((short)2);
cell_3.setCellStyle(style);
if(StrKit.isNotEmpty(record.getStr("xxmc"))){
cell_3.setCellValue(record.getStr("xxmc"));
}else{
cell_3.setCellValue("");
}
HSSFCell cell_4 = rowN.createCell((short)3);
cell_4.setCellStyle(style);
if(StrKit.isNotEmpty(record.getStr("xm"))){
cell_4.setCellValue(record.getStr("xm"));
}else{
cell_4.setCellValue("");
}
HSSFCell cell_5 = rowN.createCell((short)4);
cell_5.setCellStyle(style);
if(StrKit.isNotEmpty(record.getStr("code"))){
cell_5.setCellValue(record.getStr("code"));
}else{
cell_5.setCellValue("");
}
HSSFCell cell_6 = rowN.createCell((short)5);
cell_6.setCellStyle(style);
if(StrKit.isNotEmpty(record.getStr("userCode"))){
cell_6.setCellValue(record.getStr("userCode"));
}else{
cell_6.setCellValue("");
}
j++;
}
}catch(Exception e){
e.printStackTrace();
}
FileOutputStream fileOut = null;
try{
fileOut = new FileOutputStream(file);
wb.write(fileOut);
renderEasyUISuccess(fileName);
System.out.println(fileName);
}catch(Exception e){
e.printStackTrace();
renderEasyUISuccess("导出失败!");
}
finally{
if(fileOut != null){
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
-----------------------------------------------
导入
public void toLead() throws Exception{
UploadFile file = getFile("file");
File tempXlsFile = file.getFile();
// 从excel中读取文件
Long comId = getParaToLong("comId");
String xmlPath = PathKit.getWebRootPath() + "/zjvexml/gwh.xml";
List<?> list = EasyXls.xls2List(xmlPath, tempXlsFile);
for (Object object : list) {
Map<String, Object> map = (Map<String, Object>) object;
Record re = Db.findFirst("SELECT p.id pid ,s.xm ,x.xxmc from CttParticipant p LEFT JOIN cttxs s on p.xsId=s.id LEFT JOIN CttXX x on s.xxId =x.id where s.xm=? and x.xxmc like ? and p.competitionId = ?",map.get("xm").toString().trim(),Qb.wrapLike(map.get("xxmc").toString().trim()),comId);
if(re!=null){
re.set("code", map.get("gwh").toString().trim());
Db.update("UPDATE CttParticipant SET code = ? WHERE id = ?",map.get("gwh").toString().trim(), re.getLong("pid"));
}else{
re = Db.findFirst("SELECT p.id pid ,s.xm ,x.xxmc from CttParticipant p LEFT JOIN cttxs s on p.xsId=s.id LEFT JOIN CttXX x on s.xxId =x.id where s.xm=? and p.competitionId = ?",map.get("xm").toString().trim(),comId);
if(re!=null){
re.set("code", map.get("gwh").toString().trim());
Db.update("UPDATE CttParticipant SET code = ? WHERE id = ?",map.get("gwh").toString().trim(), re.getLong("pid"));
}
}
}
tempXlsFile.delete();
renderEasyUISuccess("导入成功!");
}
标签:
原文地址:http://www.cnblogs.com/xvguang/p/5424704.html