您的位置:首页 > 数据库

从数据库导出数据生成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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息