j2EE技术研究

public ActionForward selectExpPsdtbListToTally(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log
.debug("Entering 'com.jcompass.iams.finance.action.ExpPsdTbAction.selectExpPsdtbListToTally()' method");
}
ExpPsdtbService expPsdtbService = (ExpPsdtbService) getBean("expPsdtbService");
ExpPsdTb expPsdTb = new ExpPsdTb();
BeanUtils.copyProperties(expPsdTb, (ExpPsdTbForm) form);
// 记账时只能看到“,2-复核批准状态, 3-报销完成状态”
expPsdTb.setExpStatus("'2','3'");
SecurityUser user = AcegiContext.getCurrentUser();
// 机构级别
int bank_Lvl = user.getBank_Lvl();
/* 如果机构级别为部门(即>=2),那么只能查询自己录入的信息 */
if (bank_Lvl >= 2) {
String inputEmpId = user.getUser_Id();
expPsdTb.setInputEmpId(inputEmpId);
}
// 计算“非正常性费用明细信息”的记录数
int count = expPsdtbService.countExpPsdtb(expPsdTb);
// 分页浏览定义
Pager pager = new Pager(request, Constants.DEFAULT_PAGE_SIZE, count);
List listInfo = expPsdtbService.selectExpPsdtb(expPsdTb, pager
.getStartRow(), Constants.DEFAULT_PAGE_SIZE);
pager.setCurrentCount(listInfo.size());
// 发送内容到页面

request.setAttribute("listInfo", listInfo);

return mapping.findForward("selectExpPsdtbListToTallyResult");
}

// 导出Excel表格
public ActionForward WriteExcelToOut(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
if (log.isDebugEnabled()) {
log
.debug("Entering 'com.jcompass.iams.finance.action.ExpPsdTbAction.selectExpPsdtbListToTally()' method");
}
ExpPsdtbService expPsdtbService = (ExpPsdtbService) getBean("expPsdtbService");
ExpPsdTb expPsdTb = new ExpPsdTb();
//HttpSession session=request.getSession();
//=session.getServletContext();
ServletContext app =servlet.getServletContext();
String Path = app.getRealPath ("/");
//String path = request.getContextPath();
//String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
// 查询出所有的数据;
List list = expPsdtbService.selectExpPsdtb(expPsdTb, 0, 50000);
// 对数据进行解密;
ExpPasswordService expP = (ExpPasswordService) getBean("expPasswordService");
ExpPassword expPassword = new ExpPassword();
List list1 = expP.selectExpPasswordFirst(expPassword);
expPassword = (ExpPassword) list1.get(0);

try {
DES des = new DES(expPassword.getSecretKey());

// 解密
System.out.println("find*******************************");
Bytes by = new Bytes();

for (int i = 0; i < list.size(); i++) {
expPsdTb = (ExpPsdTb) list.get(i);
System.out.println("数据库中 name:" + expPsdTb.getCustNames());

System.out.println("数据库中 DocDes描述:" + expPsdTb.getDocDes());
// 读取数据库中的加密数据;并进行解密;生产16进制
String b1 = des.decrypt(expPsdTb.getCustNames());

System.out.println("=name 解密后的16进制码:" + b1);
// 将16进制转换成汉字;
String b2 = by.toStringHexTest(b1);
System.out.println("**name 解密后的16进制码*转换成汉字:" + b2);
expPsdTb.setCustNames(b2);
}
System.out.println("*******************************");
// getServletContext().getRealPath("/");
//String path = request.getContextPath();
//System.out.println(path + "llllllllll");
OutData out = new OutData();
//out.exportData("E:/workspace/finance/ExpPsdTb", list);

out.exportData(Path+"/temp/ExpPsdTb", list);
// response.setContentType("APPLICATION/OCTET-STREAM");
// response.setHeader("Content-Disposition","attachment;
// filename=ExpPsdTb.xls");
// response.getWriter().print(list);
// response.reset();
// response.setContentType("application/msexcel;charset=MS932");
// response.setHeader("Content-disposition",
// "attachment;filename=ExpPsdTb.xls" );
FileInputStream fis = null;
//  ServletOutputStream fout = null;
ServletOutputStream fout = null;
//路径

String filepath = Path+"/temp/ExpPsdTb.xls";
response.reset();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/octet-stream;charset=GBK");
response.setHeader("Content-disposition",
"attachment;filename=Exp.xls");
//  String filepath = "E:/workspace/finance/ExpPsdTb.xls";
//  response.reset();
//  response.setHeader("Pragma", "No-cache");
//  response.setHeader("Cache-Control", "no-cache");
//  response.setDateHeader("Expires", 0);
// 
// response.setContentType("application/octet-stream;charset=GBK");
//  response.setHeader("Content-disposition",
//  "attachment;filename=Exp.xls");
fis = new FileInputStream(filepath);
fout = response.getOutputStream();
int byteRead;
while ((byteRead = fis.read()) != -1) {
fout.write(byteRead);
}
//  fout.write(byteRead);
fis.close();
fout.close();
// }

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

// ReadExcelToIn
public ActionForward ReadExcelToIn(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
if (log.isDebugEnabled()) {
log
.debug("Entering 'com.jcompass.iams.finance.action.ExpPsdTbAction.ReadExcelToIn()' method");
}
ExpPsdTbForm expform=(ExpPsdTbForm)form;
FormFile file=expform.getInfile();
// 构建上传目录  
//路径
ServletContext app =servlet.getServletContext();
String Path = app.getRealPath ("/");
String filepath=Path+"/temp";
File dir=new File(filepath);
File outfilepath=null;
if(!dir.exists()){
dir.mkdirs();
}
if(file.getFileSize()>0){
FileOutputStream outfile=null;
try{
byte[] b=file.getFileData();//取得上传文件
//String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis());
   String extName = file.getFileName().substring(file.getFileName().lastIndexOf("."));// 得到上传文件的扩展名  
               outfilepath  = new File(dir + File.separator + file.getFileName());  
           outfile = new FileOutputStream(outfilepath, false);  
   outfile.write(b); // 通过流将数据写入文件  
}catch(Exception e){
e.printStackTrace();
}finally {  
      if (out != null) {  

      outfile.close(); // 关闭文件输出流  
   }  
}
  }


ActionForward fl=null;
System.out.println("-----------------------------"+outfilepath+"-------获取路径-----------");
ExpPsdtbService expPsdtbService = (ExpPsdtbService) getBean("expPsdtbService");
TempExpPsdtbService tempExpPsdtbService = (TempExpPsdtbService) getBean("tempExpPsdtbService");
//String context = request.getParameter("context");

//System.out.println(context + "------------");
boolean flag = false;//成功与否标志
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String error = "";
String url=basePath+"finance/expPsdTb/inData.jsp";
System.out.println(url+"-----url地址------------------");
List<ExpPsdTb> list = null;
try {
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
outfilepath));
// 创建对工作表的引用。
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
HSSFSheet sheet = workbook.getSheet("Sheet1");
//HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();

lableA: // 这里就是循环的标签
for (int r = 1; r < rows; r++) {
// 读取左上端单元
HSSFRow row = sheet.getRow(r);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
String value = "";
lableB: // 这里就是循环的标签
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell != null) {
// 重新验证
int c1 = cell.getCellNum();
//System.out.println(c1);
    int type=cell.getCellType();
    //System.out.println(type+"--------"+cell.getCellNum()+"单元格的类型-------------------------");
    //System.out.println("....blank"+HSSFCell.CELL_TYPE_BLANK+"....boolean"+HSSFCell.CELL_TYPE_BOOLEAN+"....number"+HSSFCell.CELL_TYPE_NUMERIC+"....error"+HSSFCell.CELL_TYPE_ERROR+"-----------------333333333333333333333-------------------");
    if (c1 == 0 || c1 == 1 || c1 == 3 || c1 == 5|| c1 == 7 || c1 == 9 ) {
if (type == HSSFCell.CELL_TYPE_STRING ) {
String v = cell.getStringCellValue();
if(v!=null){
if (v.length() > 60) {
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入的字符应小于60个');window.location='"+url+"';</script>";
                                            //System.out.println(error+"-------------error-------------------------------");
TempConsole.deleteTempFile(dir);
out.print(error);

break lableA;
}
value += v + "#";
}
}else if (type == HSSFCell.CELL_TYPE_NUMERIC) {
value += (long) cell.getNumericCellValue()
+ "#";
}else if(type==HSSFCell.CELL_TYPE_BLANK){
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列不能为空');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);

break lableA;
}
} else if (c1 == 10 || c1 == 16 ) {
if (type == HSSFCell.CELL_TYPE_STRING ) {
String v = cell.getStringCellValue();

if (v.length() > 30) {
// System.out.println("输入字符应小于30个");
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入字符应小于30个');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);

break lableA;
}
value += v + "#";
}else if(type==HSSFCell.CELL_TYPE_BLANK){
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入不能为空');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);

break lableA;
}

} else if (c1 == 15 || c1 == 17 || c1 == 18 || c1 == 19) {
if (type == HSSFCell.CELL_TYPE_STRING ) {
String v = cell.getStringCellValue();

if (v.length() > 255) {
                  error = "<script>alert('您在第"+ row.getRowNum() + "行第" + (c1+1)+ "列输入字符应小于255个');window.location='"+url+"';</script>";
                  System.out.println(error+"-------------error-------------------------------");
                  TempConsole.deleteTempFile(dir);
                  out.print(error);
                 
break lableA;
}
value += v + "#";
}else if (type == HSSFCell.CELL_TYPE_NUMERIC) {
     value += (long) cell.getNumericCellValue()+ "#";
     }else if(type==HSSFCell.CELL_TYPE_BLANK){
  error = "<script>alert('您在第"+ row.getRowNum() + "行第" + (c1+1)+ "列不能为空');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);
 
      break lableA;
}

} else if (c1 == 12) {
if (type == HSSFCell.CELL_TYPE_STRING ) {
String v = cell.getStringCellValue();
if (v.length() > 255) {
//System.out.println("您输入的字符应小于255个");

error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入的字符应小于255个');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);

break lableA;
}
value += v + "#";
}else if (type == HSSFCell.CELL_TYPE_NUMERIC
|| HSSFDateUtil.isCellDateFormatted(cell)) {
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String v = sdf.format(cell
.getDateCellValue());
value += v + "#";

} else if(type==HSSFCell.CELL_TYPE_BLANK){
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入的不能为空');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);

break lableA;
}else {
//System.out.println("你输入的不是日期格式!");
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入的日期格式不对');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);

break lableA;
}
} else if (c1 == 2 || c1 == 4 || c1 == 6 || c1 == 8
|| c1 == 13 || c1 == 14 ) {
// int type=type;
//System.out.println(type+"-----------------2222222222222-------------------");
int type2=HSSFCell.CELL_TYPE_NUMERIC;
//System.out.println(type2+"-----------------2222222222222-------------------");
if (type == HSSFCell.CELL_TYPE_NUMERIC) {
value += (long) cell.getNumericCellValue()
+ "#";
} else {
//System.out.println("您输入的不是数字");
// error="<script>alert('您在第'"+row.getRowNum()+"'行第'"+c1+"'列输入的不是数字');</script>";
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入的不是数字');window.location='"+url+"';</script>";
System.out.println(error+"-------------error-------------------------------");
TempConsole.deleteTempFile(dir);
out.print(error);

break lableA;
}
} else if (c1 == 11 ) {
//int type=type;
//System.out.println(type+"---------------------------------------");

int type1=HSSFCell.CELL_TYPE_STRING;
//System.out.println(type1+"-------------------------------------");
if(type == HSSFCell.CELL_TYPE_STRING ){

String v = cell.getStringCellValue();
value += v + "#";
}
else  if (type == HSSFCell.CELL_TYPE_NUMERIC || HSSFDateUtil.isCellDateFormatted(cell)) {

// 转为yyyy-MM-dd格式
DateFormat sdf = new SimpleDateFormat(
"yyyy/MM/dd");
String v = sdf.format(cell.getDateCellValue());
value += v + "#";

} else if(type==HSSFCell.CELL_TYPE_BLANK){
error = "<script>alert('您在第"
+ row.getRowNum() + "行第" + (c1+1)
+ "列输入的不能为空');window.location='"+url+"';</script>";
   System.out.println(error+"-------------error-------------------------------");
   TempConsole.deleteTempFile(dir);
   out.print(error);

break lableA;
}
}

}
}
// 一条记录
//System.out.println("dddd" + value);
// tempExpPsdtbService

// 下面可以将查找到的行内容用SQL语句INSERT到数据库中
String[] temp = value.split("#");
System.out.println(temp.length + "----------------25");
if (temp.length == 20) {
ExpPsdTb e = new ExpPsdTb();
e.setExpBatNo(Integer.toString(row.getRowNum()));
e.setEmpId(temp[0]);
e.setName(temp[1]);
e.setNewExpMainType(temp[2]);
e.setNewExpMainDesc(temp[3]);
e.setNewExpSubType(temp[4]);
e.setNewExpSubDesc(temp[5]);
e.setOldExpMainType(temp[6]);
e.setOldExpMainDesc(temp[7]);
e.setOldExpSubType(temp[8]);
e.setOldExpSubDesc(temp[9]);
e.setSimpleMainMsg(temp[10]);
e.setExpYearMonth(temp[11]);
e.setHappenYearMonth(temp[12]);
e.setExpAmt(new Double(temp[13]));
e.setDocQuantity(new Integer(temp[14]));
e.setDocNums(temp[15]);
e.setCustCatlogId(temp[16]);
e.setCustNames(temp[17]);
e.setCustPost(temp[18]);

e.setDocDes(temp[19]);
// 插入到数据库中的临时表中
e.setInputDate("0");
e.setInputEmpId("0");
e.setExpStatus("0");
tempExpPsdtbService.insertExpPsdTb(e);
}
log.info(value);
}
} // 行循环结束
ExpPsdTb expPsdTb = new ExpPsdTb();
SysJrnCtrlService sysJrnCtrlService = (SysJrnCtrlService) getBean("sysJrnCtrlService");
list = tempExpPsdtbService.selectExpPsdtb(expPsdTb, 0, 50000);
// 插入成功与否标志

int count = list.size();
if (count == (rows - 1)) {
// 批量把临时表数据插入到数据库中
// 生成“报销凭证号码”
SysJrnCtrl sysJrnCtrl1 = new SysJrnCtrl();
sysJrnCtrl1.setBusiModType("E");

SysJrnCtrl sysJrnCtrl = sysJrnCtrlService
.selectSysJrnCtrl(sysJrnCtrl1);
int sysno = Integer.parseInt(sysJrnCtrl.getSeqNo().trim());

sysJrnCtrl
.setSeqNo(Integer
.toString(
(Integer.parseInt(sysJrnCtrl.getSeqNo()
.trim()) + count)).trim());
sysJrnCtrlService.updateSysJrnCtrl(sysJrnCtrl);
for (ExpPsdTb ee : list) {
sysno++;
// 用序列号作为报销凭证号;

ee.setExpBatNo(sysJrnCtrl.getBusiModType() + sysno);
/* 录入用户 */
SecurityUser user = AcegiContext.getCurrentUser();
String inputEmpId = user.getUser_Id();
ee.setInputEmpId(inputEmpId);
/* 录入日期 格式:2009-01-02 */
String yyyyMmDd = DateUtil.getCurrentDateYYYYMMDD();
String yyyy = yyyyMmDd.substring(0, 4);
String mm = yyyyMmDd.substring(4, 6);
String dd = yyyyMmDd.substring(6, ;
String inputDate = yyyy + "/" + mm + "/" + dd;
ee.setInputDate(inputDate);
// 新增数据默认为“登记状态(0)”
ee.setExpStatus("0");
// 加密
ExpPasswordService expP = (ExpPasswordService) getBean("expPasswordService");
ExpPassword expPassword = new ExpPassword();
List list1 = expP.selectExpPasswordFirst(expPassword);
expPassword = (ExpPassword) list1.get(0);
DES des = new DES(expPassword.getSecretKey());
Bytes by = new Bytes();
System.out.println("form  表单中 name:" + ee.getCustNames());
System.out.println("form  表单中 DocDes描述:" + ee.getDocDes());
// 将汉字转换成16进制
String b1 = by.Jiama(ee.getCustNames());
System.out.println("*name 转换成16进制:" + b1);
// 对16进制进行加密
String b2 = des.encrypt(b1);
System.out.println("**name 转换成16进制*加密的密文:" + b1);
ee.setCustNames(b2);
expPsdtbService.insertExpPsdTb(ee);
flag = true;
}
if (flag == true) {
// 删除临时表中的数据

tempExpPsdtbService.deleteExpPsdTb();
TempConsole.deleteTempFile(dir);
error = "<script>alert('恭喜你,你的数据导入成功!!!');window.location='"+url+"';</script>";

out.print(error);
} else {
// 删除临时表中的数据
                   
tempExpPsdtbService.deleteExpPsdTb();
                    //删除临时文件
TempConsole.deleteTempFile(dir);
   
error = "<script>alert('对不起,你的数据导入失败!!!');window.location='"+url+"';</script>";

out.print(error);
}
} else {
// 删除临时表中的数据
TempConsole.deleteTempFile(dir);
tempExpPsdtbService.deleteExpPsdTb();

}

} catch (Exception e) {
System.out.println(e);
// 删除临时表中的数据

tempExpPsdtbService.deleteExpPsdTb();

error = "<script>alert('对不起,你的数据导入失败!!!');window.location='"+url+"';</script>";

out.print(error);
}
// ********************************************
return null;
}

}

你可能感兴趣的:(sql,jsp,cache,servlet,Excel)