public void uploadToBank() throws Exception {
JSONObject json = new JSONObject();
boolean success = false;
HashMap
String str = request.getParameter("str") + "";
List
HSSFWorkbook workBook = null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
try {
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String fileName = qiming+ ".xls";
File file = new File(fileName);
FtpService ftp = new FtpService();
// 重新复制模版文件
String tempUrl = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/temp/bgdlist.xls";
FileUtils.copyFile(new File(tempUrl), file);
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
// 生成一个表格
HSSFSheet sheet = workBook.getSheetAt(0);
HSSFCellStyle style = workBook.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font = workBook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
// 数字样式
HSSFCellStyle numstyle = workBook.createCellStyle();
numstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
numstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
numstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
numstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
numstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
numstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 把字体应用到当前的样式
numstyle.setFont(font);
numstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
// 产生表格表头列标题行
HSSFRow row = null;
int rowIndex = 2;
// 取值的属性名
String[] fields = { "jgdm", "bgsj", "bgdh", "bz", "yfje", "zje" };
// 遍历集合数据,产生数据行
Iterator
while (it.hasNext()) {
row = sheet.createRow(rowIndex);
BgdView t = it.next();
// 利用反射,根据javabean属性属性名称,获得得到属性值
Class> cla = t.getClass();
for (int i = 0; i < fields.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
Field field = cla.getDeclaredField(fields[i]);
field.setAccessible(true);
Object value = field.get(t);
String textValue = value.toString();
// 判断是否数字
if (textValue != null && !"".equals(textValue)) {
if (fields[i].contains("je")) {
// 是数字当作double处理
cell.setCellStyle(numstyle);
cell.setCellValue(Double.parseDouble(textValue));
} else {
cell.setCellValue(textValue);
}
}
}
rowIndex++;
}
// 得到输入流
out = new ByteArrayOutputStream();
workBook.write(out);
byte[] b = out.toByteArray();
in = new ByteArrayInputStream(b);
Map
String pathFile = ftp.getAdfilePath();
mapftp.put(pathFile + fileName, in);
ftp.upload(mapftp);
in.close();
map.put(fileName, fileName);
success = true;
} catch (Exception e) {
e.printStackTrace();
success = false;
}
json.put("success", success);
json.put("map", map);
response.setContentType("text/html; charset=UTF-8");
JsonResponse.write(json.toString(), response);
}