// 創建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);