package com.chinova.test; html
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL; java
import javax.imageio.ImageIO; c++
import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress; apache
/**
* POI導出Excel文件
* @author shaohua.zou
*
* @version 2013-9-16 下午1:41:18
*/
public class ExcelExport { dom
/**
* @param args
*/
public static void main(String[] args) {
excelExport();
} ui
public static void excelExport() { url
final int rowNum = 27;
final int colNum = 15;
HSSFWorkbook wb = null;
Sheet sheet = null; .net
String today = "2013/8/31";
String sign = "Month to date"; excel
String[] titles = { "", "", "", "Chinapay eMail\r\n 商城總計",
"Japan Page\r\n 日本館首頁", "Taiwan Page\r\n 臺灣館首頁",
"USA Page\r\n 美國館首頁", "Anhui Page\r\n 安徽館首頁",
"China Page\r\n 中國館首頁" };
String[] colNames = { "", "Page View (PV)\r\n 瀏覽量",
"Unique Visitor (UV)\r\n 獨立訪客", "Completed Orders\r\n 確認訂單",
"Transaction Amount\r\n 交易金額", htm
"1st Top Seller\r\n 最佳銷量", "Unit Price 單價", "Qty Sold 銷量",
"2nd Top Seller\r\n 第二銷量", "Unit Price 單價", "Qty Sold 銷量",
"3rd Top Seller\r\n 第三銷量", "Unit Price 單價", "Qty Sold 銷量",
"1st Top Seller\r\n 最佳銷量", "Unit Price 單價", "Qty Sold 銷量",
"2nd Top Seller\r\n 第二銷量", "Unit Price 單價", "Qty Sold 銷量",
"3rd Top Seller\r\n 第三銷量", "Unit Price 單價", "Qty Sold 銷量" };
int n = 0;
int len = 1;
String fileName = "D:/日報.xls";
File f = new File(fileName);
ByteArrayOutputStream byteArrayOut = null ;
BufferedImage bufferImg =null;
String[] jpgUrls = {"http://img.chinapay.com/data/files/store_37452/goods_93/small_201303271804531386.jpg",
"http://img.chinapay.com/data/files/store_44066/goods_37/201308280953576580.jpg",
"http://img.chinapay.com/data/files/store_289253/goods_95/small_201309031434558044.jpg",
"http://img.chinapay.com/data/files/store_289253/goods_180/small_201309031403003861.jpg",
"http://img.chinapay.com/data/files/store_37452/goods_98/small_201309121508186810.jpg",
"http://img.chinapay.com/data/files/store_37452/goods_24/small_201301241133447193.jpg"
};
String[] https = {"http://emall.chinapay.com/goods/37452/1010000109792.html",
"http://emall.chinapay.com/goods/44066/1010000119323.html",
"http://emall.chinapay.com/goods/289253/1010000119621.html?jpsv=laoxcashback6",
"http://emall.chinapay.com/goods/289253/1010000119627.html?jpsv=laoxcashback6",
"http://emall.chinapay.com/goods/37452/1010000120588.html",
"http://emall.chinapay.com/goods/37452/1010000107096.html"};
URL url = null ;
HSSFHyperlink link = null ;
HSSFPatriarch patri = null;
HSSFClientAnchor anchor = null;
try {
if (!f.exists()) {
wb = new HSSFWorkbook();
} else {
FileInputStream in = new FileInputStream(fileName);
wb = new HSSFWorkbook(in);
}
CellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFCellStyle.THIN_BACKWARD_DIAG);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// style.setLeftBorderColor(HSSFColor.RED.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
style.setWrapText(true);
sheet = wb.createSheet("sheet " + ((int) (100000 * Math.random())));
// 設置列的寬度
sheet.setDefaultColumnWidth(20);
sheet.setDefaultRowHeight((short) 400);
Row row = null;
Cell cell = null;
for (int r = 0; r < rowNum; r++) {
row = sheet.createRow(r);
//設置第1行當高度
if(r==0){
row.setHeightInPoints(30);
}
//設置第2列之後的寬度(即列號>=2的列,列號從0開始)
if(r >= 2){
sheet.setColumnWidth(r, 3020);
}
for (int c = 0; c < colNum; c++) {
cell = row.createCell(c);
cell.setCellStyle(style);
// 處理第一行
if (r == 0) {
sheet.addMergedRegion(new CellRangeAddress(r, r, 3, 4));
sheet.addMergedRegion(new CellRangeAddress(r, r, 5, 6));
sheet.addMergedRegion(new CellRangeAddress(r, r, 7, 8));
sheet.addMergedRegion(new CellRangeAddress(r, r, 9, 10));
sheet.addMergedRegion(new CellRangeAddress(r, r, 11, 12));
sheet.addMergedRegion(new CellRangeAddress(r, r, 13, 14));
if (c < 3) {
cell.setCellValue(titles[n++]);
} else {
if ((c & 1) == 1) {
System.out.println("c===" + c);
cell.setCellValue(titles[n++]);
}
}
}
// 處理第2~8行
if (r > 0 && r <= 8) {
if (c == 0) {
if (r < 8 && (r & 1) == 1) {
sheet.addMergedRegion(new CellRangeAddress(r,r + 1, 0, 0));
System.err.println("row----->" + r+ " len----->" + (len));
cell.setCellValue(colNames[len++]);
} else if (r > 8) {
System.out.println("len+++++++++>" + (len));
cell.setCellValue(colNames[len++]);
}
} else if (c == 1) {
cell.setCellValue((r & 1) == 1 ? today : sign);
System.err.println("r---->" + r);
} else if (c == 2) {
cell.setCellValue((r & 1) == 1 ? "當天" : "當月");
} else {
if((c&1)==1){
sheet.addMergedRegion(new CellRangeAddress(r, r, c, c + 1));
cell.setCellValue("26.55");
}
}
}
// 處理第8行之後的數據(不包括第8行)
if (r > 8) {
//設置列高(圖片的高度)
if (r % 3 == 0) {
sheet.getRow(r).setHeightInPoints(110);
}
if (c == 0) {
System.err.println("r---->" + r);
cell.setCellValue(colNames[r - 4]);
} else if (c == 1) {
cell.setCellValue((r % 3) == 0 ? today: (r % 3 == 1 ? "PV 瀏覽量" : "Total Sales 總額"));
} else if (c == 2) {
if (r % 9 == 0) {
sheet.addMergedRegion(new CellRangeAddress(r,r + 8, c, c));
if (r / 9 == 1)
cell.setCellValue("當天");
else
cell.setCellValue("當月");
cell.setCellStyle(style);
}
} else {
if (r % 3 == 0) {
if ((c & 1) == 1) {
sheet.addMergedRegion(new CellRangeAddress(r, r, c, c + 1));
//添加遠程圖片信息
url = new URL(jpgUrls[(c-3)/2]);
bufferImg = ImageIO.read(url.openStream());
byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferImg, "jpg", byteArrayOut);
patri = (HSSFPatriarch) sheet.createDrawingPatriarch();
anchor = new HSSFClientAnchor(10, 2, 0, 0,(short) c, r, (short) (c+2), r+1);
patri.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
bufferImg.flush();
// link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
// System.out.println(https[(c-3)/2]);
// link.setAddress("fetion/"+https[(c-3)/2]);
// cell.setHyperlink(link);
// link = (HSSFHyperlink) cell.getHyperlink();
// link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
// link.setAddress(https[(c-3)/2]);
// cell.setHyperlink(link);
}
} else {
if((c&1)==0){
link = wb.getCreationHelper().createHyperlink(Hyperlink.LINK_URL);
link.setAddress(https[(c-3)/2]);
cell.setHyperlink(link);// 設定單元格的連接
cell.setCellValue("圖片超連接");
}else{
cell.setCellValue("Number");
}
}
}
}
}
}
// 備註
row = sheet.createRow(27);
cell = row.createCell(0);
sheet.addMergedRegion(new CellRangeAddress(27, 27, 0, colNum - 1));
cell.setCellValue("* 銷量排名不以銷售金額計算,如相同銷量者,則以PV量少者爲優勝");
FileOutputStream out = new FileOutputStream(fileName);
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("++++++++++++ EXCEl文件 success +++++++++++++");
}
}
---------------------------------------------------------------------------------------------
一、本想在圖片上增長超連接,沒成功。如今的狀況是,要麼有圖片沒連接,要麼有連接沒圖片。
若有能實現圖片上增長超連接,還請不吝賜教,謝謝了!