1. 程式人生 > >從資料庫匯出資料生成excel後放置到ftp伺服器(ftp,sftp協議)

從資料庫匯出資料生成excel後放置到ftp伺服器(ftp,sftp協議)

// 建立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("ID 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);

相關推薦

資料庫匯出資料生成excel放置ftp伺服器ftp,sftp協議

// 建立Excel的工作書冊 Workbook,對應到一個excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 建立Excel的工作sheet,對應到一

Python資料庫匯出資料excel

from trump.db import query from utils.result_process import success,aborted import xlwt import datetime import config_business from sanic

Spring boot實現匯出資料生成excel檔案返回

一、基於框架 1.IDE IntelliJ IDEA 2.軟體環境 Spring boot mysql mybatis org.apache.poi 二、環境整合 1.建立spring boot專案工程 略

如何資料庫調出資料顯示到頁面 PHP+Mysql+Html簡單例項

首先顯示一下資料庫的資料 頁面資訊 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> &l

資料庫匯出資料excel的附件形勢傳送到指定郵箱

        SysRunLog objSysRunLog = SysRunLog.GetInstance();         private Int16 daynum = Convert.ToInt16(ConfigurationSettings.AppSetting

javascript資料庫獲取資料生成EasyUI樹

不用管是什麼資料庫,php還是java語言只要資料庫表字段是有自身指向的結構的都行。廢話少說,直接上程式碼<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl

python資料庫請求資料給到前端的具體實現

先來貼一竄程式碼讓大家理解前端/後端/資料庫的工作原理, 首先簡要說明:前端向後端請求資料,後端根據前端請求資料的類別分析其需求,並連線到資料庫獲取相應資料: 來一段簡單的例項程式碼模擬淘寶商城: 前端程式碼: <!DOCTYPE html> <html> &

php - 資料庫匯出百萬級資料(CSV檔案)

將資料庫連線資訊、查詢條件、標題資訊替換為真實資料即可使用。 <?php set_time_limit(0); ini_set('memory_limit', '128M'); $fileName = date('YmdHis', time()); header('Content-

Python模組-xlwt模組 Django資料庫匯出Excel

xlwt模組 xlwt模組說明 xlwt是一個庫,用於向舊的Excel檔案(即.xls)寫入資料和格式化資訊。 資料說明 https://xlwt.readthedocs.io/en/latest/ 安裝模組 使用pip安裝 pip install xlwt 官方地址 https://pypi.org

Java呼叫R ,資料庫讀取資料,用R根據讀取的資料生成圖片儲存

Java呼叫R 以及用R生成的圖片儲存 使用 png()可以保證圖片質量 有人說繪製的圖片上有中文會出現亂碼,其實設定下字型就好了 family=‘STXihei’ Java呼叫R 以及用R生成的圖片儲存 Rserve的配置和啟動: ①安裝Rserve包。

powerDesigner 資料庫匯出表結構,並生產excel ,有效

將資料庫中的表匯入到PowerDesigner中並轉為excel文件 1、開啟PowerDesigner12,在選單中按照如下方式進行操作    file->Reverse Engineer->DataBase    點選後,彈出 New Physical D

資料庫讀取資料動態生成樹形選單示例

用C#從資料庫讀取資料,動態生成樹形選單例子 資料庫表 前臺程式碼 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="demo.aspx.cs" Inherits="demo" %> <!DO

獲取jqGrid中的所有資料匯出生成Excel檔案流供使用者下載(post請求實現檔案下載)

最近有一個需求是: 將jqGrid表格中的資料生成報表Excel檔案返回給使用者。 我的想法是獲取jqGrid中的資料然後生成json資料,傳給後端,生成檔案流,響應到前端,儲存為excel檔案。 ajax為什麼不能夠實現下載檔案 ajax的返回值型別是json,text

PowerDesigner資料庫匯出pdm以及匯出html格式資料字典

PowerDesigner從資料庫匯出pdm File ---> Reverse Engineer---> Database---> DBMS(選對應的資料庫,這裡是ORACLE Version 10g),選中Share the DBMS Defini

第一篇 部落格:java資料庫讀取資料,並寫入到excel表格中

  今天,組長分配了查詢資料庫記錄,並把這些記錄寫入到excel表格中,以前沒有嘗試過,借鑑了一些別人的程式碼,最終實現了功能,寫一篇部落格,總結一下這個過程。1.準備需要用到的jar包    1.jxl.jar     2.mysql-connector-java-5.1.

下拉選單資料庫讀取資料動態生成

一、頁面 index.ftl <html> <head> <meta charset="utf-8"> <script type="text/javascript" src

ASP.NET DataSet資料生成Excel文件匯出下載

public partial class am_ElectronicInvoinceExportExcel : BasePage { protected void Page_Load(object sender, EventArgs e)

網頁上實現匯出excel表格時,怎樣在後臺生成excel,讓表格在瀏覽器中自動下載larval框架

Html程式碼片段: <form method="post" id="export_form" action="action" style="float: left">     <input id="export_info" type="hidden" n

SQL server 匯出資料之後身份證四位全變成0的問題

負責一個專案的時候經常會遇到匯出資料的問題,而且每次匯出的欄位都不一樣,於是總結出了一個很方便的辦法,並且匯出來的資料不會變成科學計數,身份證後四位也不會全變成0,下面是步驟: 第一步:開啟SQL server,用select語句把需要匯出的資料查詢出來。 第二步:查詢所

php匯出資料excel檔案

<?php require_once "../excel/PHPExcel.php"; require_once "../excel/PHPExcel/Writer/Excel2007.php"; require_once "../config/database.php"; requir