从数据库导出数据生成excel后放置到ftp服务器(ftp,sftp协议)
2018-03-19 16:05
423 查看
// 创建Excel的工作书册 Workbook,对应到一个excel文档 HSSFWorkbook wb = new HSSFWorkbook(); // 创建Excel的工作sheet,对应到一个excel文档的tab HSSFSheet sheet = wb.createSheet(); // 设置excel每列宽度 sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(1, 4000); sheet.setColumnWidth(2, 4000); sheet.setColumnWidth(3, 4000); sheet.setColumnWidth(4, 4000); sheet.setColumnWidth(5, 4000); sheet.setColumnWidth(6, 4000); sheet.setColumnWidth(7, 4000); sheet.setColumnWidth(8, 4000); sheet.setColumnWidth(9, 4000); // 创建字体样式 HSSFFont font = wb.createFont(); font.setFontName("Verdana"); font.setBoldweight((short) 100); font.setFontHeight((short) 300); font.setColor(HSSFColor.BLUE.index); // 创建单元格样式 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 设置边框 style.setBottomBorderColor(HSSFColor.RED.index); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setFont(font);// 设置字体 /*// 创建Excel的sheet的一行 HSSFRow row = sheet.createRow(0); row.setHeight((short) 500);// 设定行的高度 // 创建一个Excel的单元格 HSSFCell cell = row.createCell(0); // 合并单元格(startRow,endRow,startColumn,endColumn) sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 11)); // 给Excel的单元格设置样式和赋值 cell.setCellStyle(style); String title = "用户列表"; cell.setCellValue(title );*/ // 设置单元格内容格式时间 HSSFCellStyle style1 = wb.createCellStyle(); style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyy-mm-dd")); style1.setWrapText(true);// 自动换行 style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle style2 = wb.createCellStyle(); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFRow row = sheet.createRow(0); row.setHeight((short) 500);// 设定行的高度 HSSFCell cell = row.createCell(0);; cell.setCellStyle(style); cell.setCellStyle(style2); cell.setCellValue("No"); cell = row.createCell(1); cell.setCellStyle(style2); cell.setCellValue("I 4000 D of items"); cell = row.createCell(2); cell.setCellStyle(style2); cell.setCellValue("Origin Country"); cell = row.createCell(3); cell.setCellStyle(style2); cell.setCellValue("Name of senders"); cell = row.createCell(4); cell.setCellStyle(style2); cell.setCellValue("Destination country"); cell = row.createCell(5); cell.setCellStyle(style2); cell.setCellValue("Name of recipients"); cell = row.createCell(6); cell.setCellStyle(style2); cell.setCellValue("Addresses of recipients"); cell = row.createCell(7); cell.setCellStyle(style2); cell.setCellValue("Content of items"); cell = row.createCell(8); cell.setCellStyle(style2); cell.setCellValue("Weight Kg"); cell = row.createCell(9); cell.setCellStyle(style2); cell.setCellValue("Declared value of conten USD"); for (int j = 0; j <= innerorders.size()-1 ; j++) { InnerOrder innerOrder = innerorders.get(j); OmsTradeOrder omstradeorder = JSONObject.parseObject(innerOrder.getSendManifestData(), OmsTradeOrder.class); int i = 0; row = sheet.createRow(j+1); cell = row.createCell(i); cell.setCellStyle(style2); cell.setCellValue(j+1); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(innerOrder.getMailNo()); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(omstradeorder.getShipper().getCountry()); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(omstradeorder.getShipper().getName()); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(omstradeorder.getConsignee().getCountry()); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(omstradeorder.getConsignee().getName()); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(omstradeorder.getConsignee().getProv()+" "+omstradeorder.getConsignee().getCity()+" "+omstradeorder.getConsignee().getAddress1()); cell = row.createCell(++i); cell.setCellStyle(style2); List<OrderItem> items = omstradeorder.getItems(); String itemname=""; Integer a=0; for(OrderItem orderItem:items){ if(a==0){ itemname=itemname+orderItem.getName(); a=a+1; }else{ itemname=itemname+"&"+orderItem.getName(); } } cell.setCellValue(itemname); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(String.valueOf(omstradeorder.getBasicInfo().getWeight())); cell = row.createCell(++i); cell.setCellStyle(style2); cell.setCellValue(omstradeorder.getBasicInfo().getGoodsValue()); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String excel_name = sdf.format(new Date())+".xlsx";
以上是创建一个名为wb的电子excel表格
二: 下面是两种协议的ftp服务器
String ftpUrl = StaticResource.getProMap().get("url");//ftp服务器地址 String ftpUser = StaticResource.getProMap().get("user");//ftp服务器用户名 String ftpPass = StaticResource.getProMap().get("password");//ftp服务器密码
1 sftp协议,默认端口22
public ChannelSftp connect(String host, int port, String username, String password) { ChannelSftp sftp = null; try { JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession = jsch.getSession(username, host, port); System.out.println("Session created."); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); System.out.println("Session connected."); System.out.println("Opening Channel."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; System.out.println("Connected to " + host + "."); } catch (Exception e) { } return sftp; } ChannelSftp sftp = connect(ftpUrl, 22, ftpUser, ftpPass); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { wb.write(os); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] b = os.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(b); sftp.cd(StaticResource.getProMap().get("doc"));//进入放置文件夹路径下 //boolean res = ftp.uploadFile(excel_name, in); try { sftp.put(in, excel_name); } catch (Exception e) { e.printStackTrace(); }
2 ftp协议
FtpUtil ftp = new FtpUtil(ftpUrl, 22, ftpUser, ftpPass, "文件夹路径"); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { wb.write(os); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] b = os.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(b); boolean res = ftp.uploadFile(excel_name , in);
相关文章推荐
- PHP将操作数据库得到的数据生成Excel导出
- phpexcel导出数据库数据生成excel文件
- delhi从数据库导出数据生成Excel并自动发送邮件笔记
- 数据库中数据如何导出生成Excel?
- 从数据库导出数据生成word或excel文档
- poi+jdbc实现从数据库中导出表的数据字典结构(单个sheet和多个sheet供你选择),并且生成excel文档,作者:vipyhd
- 依赖POI实现EXCEL导入数据并生成javaBean和EXCEL根据数据库表导出
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之二——使用Action
- SqlServer 2008数据库 怎么样导出Excel表格数据
- 从数据库导出数据到excel并且保存到指定位置
- Java Web利用POI导出Excel的示例(不在数据库取数据,Map类型的数据)
- Gradview绑定数据库数据并导出Excel
- PHP----从数据库导出数据到表格:PHPExcel
- PHP导出数据表数据生成Excel表格文件
- 如何将C#中将TextBox的数据以及从数据库中查询得到的数据导出到Excel中
- C#数据库数据导入导出系列之三 数据库导出到Excel下
- C#数据库数据导入导出系列之二 数据库导出到Excel上
- java将数据库里的数据导出到excel
- 项目实训涉及的获取数据库数据,导出生成Excel表格(使用Nutz框架)
- 一些关于从数据库导出数据到word、excel、.txt文本文件的代码