java操作Excel之jxl----簡單上手
阿新 • • 發佈:2019-01-17
先建立一個Excel:
package com.kk.jxl;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.kk.DButils.DBData;
import jxl.CellType;
import jxl.Workbook;
import jxl.format .Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write .WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* 積分和抽獎.xls
* @author kk
*/
public class ExchangeExcel {
public static WritableWorkbook book = null;
public static WritableSheet sheet = null;
@SuppressWarnings({ "unused", "static-access" })
public static void excel() throws IOException, RowsExceededException, WriteException{
//建立一個Excel檔案
book = Workbook.createWorkbook(new File("D://img//積分和抽獎.xls"));
sheet = book.createSheet("積分和抽獎", 0);
//建立字型物件
WritableFont titleFont = new WritableFont(WritableFont.createFont("微軟雅黑"), 14, WritableFont.NO_BOLD);
//標題樣式
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
//行樣式
WritableCellFormat contentFormat = new WritableCellFormat();
//設定單元格的樣式
WritableFont contentFont = new WritableFont(WritableFont.createFont("微軟雅黑"), 10, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE);
WritableCellFormat contentFormat2 = new WritableCellFormat(contentFont);
//設定邊框
contentFormat2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN
,jxl.format.Colour.BLACK);
//設定自動換行
contentFormat2.setWrap(true);
//字型加粗
contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
// 設定格式居中對齊
titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
contentFormat.setAlignment(jxl.format.Alignment.CENTRE);
contentFormat2.setAlignment(jxl.format.Alignment.CENTRE);
//設定單元格顏色
WritableFont font = new WritableFont(WritableFont.createFont("微軟雅黑"), 10, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE);
Color color = Color.decode("#cbccff");
book.setColourRGB(Colour.ORANGE, color.getRed(), color.getGreen(),color.getBlue());
WritableCellFormat wcf = new WritableCellFormat(font);
wcf.setBackground(Colour.ORANGE);
//設定格式居中對齊
wcf.setAlignment(jxl.format.Alignment.CENTRE);
//設定邊框
wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN
,jxl.format.Colour.BLACK);
//垂直居中樣式
WritableFont verticalfont = new WritableFont(WritableFont.createFont("微軟雅黑"), 10, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE);
WritableCellFormat verticalwcf = new WritableCellFormat(verticalfont);
verticalwcf.setVerticalAlignment(VerticalAlignment.CENTRE);
//自動換行
verticalwcf.setWrap(true);
/**合併單元格**/
sheet.mergeCells(0, 0, 13, 0);
sheet.mergeCells(0, 2, 0, 4);
sheet.mergeCells(1, 2, 1, 4);
sheet.mergeCells(0, 5, 0, 7);
sheet.mergeCells(1, 5, 1, 7);
sheet.mergeCells(3, 1, 4, 1);
sheet.mergeCells(3, 2, 4, 2);
sheet.mergeCells(3, 3, 4, 3);
sheet.mergeCells(3, 4, 4, 4);
sheet.mergeCells(3, 5, 4, 5);
sheet.mergeCells(3, 6, 4, 6);
sheet.mergeCells(3, 7, 4, 7);
sheet.mergeCells(5, 1, 6, 1);
sheet.mergeCells(5, 2, 6, 2);
sheet.mergeCells(5, 3, 6, 3);
sheet.mergeCells(5, 4, 6, 4);
sheet.mergeCells(5, 5, 6, 5);
sheet.mergeCells(5, 6, 6, 6);
sheet.mergeCells(5, 7, 6, 7);
sheet.mergeCells(7, 1, 13, 1);
sheet.mergeCells(7, 2, 13, 2);
sheet.mergeCells(7, 3, 13, 3);
sheet.mergeCells(7, 4, 13, 4);
sheet.mergeCells(7, 5, 13, 5);
sheet.mergeCells(7, 6, 13, 6);
sheet.mergeCells(7, 7, 13, 7);
//新增邊框
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 14; i++) {
sheet.addCell(new Label(i,j,"",contentFormat2));
}
}
//標題
sheet.addCell(new Label(0, 0,"積分兌獎 ",contentFormat2));
sheet.addCell(new Label(0, 1,"序號",contentFormat2));
sheet.addCell(new Label(1, 1,"應用名稱",contentFormat2));
sheet.addCell(new Label(3, 1,"兌換使用者數",contentFormat2));
sheet.addCell(new Label(5, 1,"兌換筆數",contentFormat2));
sheet.addCell(new Label(7, 1,"兌換積分數",contentFormat2));
sheet.addCell(new Label(0, 2,"1",contentFormat2));
sheet.addCell(new Label(0, 5,"2",contentFormat2));
sheet.addCell(new Label(1, 2,"積分(duijiang)",contentFormat2));
sheet.addCell(new Label(1, 5,"企業積分(jfdh_qy)",contentFormat2));
sheet.addCell(new Label(2, 2,"本月",contentFormat2));
sheet.addCell(new Label(2, 3,"本年",contentFormat2));
sheet.addCell(new Label(2, 4,"總計",contentFormat2));
sheet.addCell(new Label(2, 5,"本月",contentFormat2));
sheet.addCell(new Label(2, 6,"本年",contentFormat2));
sheet.addCell(new Label(2, 7,"總計",contentFormat2));
//抽獎平臺
//新增邊框
for (int j = 14; j < 19; j++) {
for (int i = 0; i < 14; i++) {
sheet.addCell(new Label(i,j,"",contentFormat2));
}
}
//合併單元格
sheet.mergeCells(0, 14, 13, 14);
sheet.mergeCells(0, 16, 0, 18);
sheet.mergeCells(2, 15, 4, 15);
sheet.mergeCells(2, 16, 4, 16);
sheet.mergeCells(2, 17, 4, 17);
sheet.mergeCells(2, 18, 4, 18);
sheet.mergeCells(5, 15, 6, 15);
sheet.mergeCells(5, 16, 6, 16);
sheet.mergeCells(5, 17, 6, 17);
sheet.mergeCells(5, 18, 6, 18);
sheet.mergeCells(7, 15, 13, 15);
sheet.mergeCells(7, 16, 13, 16);
sheet.mergeCells(7, 17, 13, 17);
sheet.mergeCells(7, 18, 13, 18);
//標題
sheet.addCell(new Label(0, 14,"抽獎平臺",contentFormat2));
sheet.addCell(new Label(0, 15,"序號 ",contentFormat2));
sheet.addCell(new Label(0, 16,"1",contentFormat2));
sheet.addCell(new Label(1, 16,"本月",contentFormat2));
sheet.addCell(new Label(1, 17,"本年",contentFormat2));
sheet.addCell(new Label(1, 18,"總計",contentFormat2));
sheet.addCell(new Label(2, 15,"參與抽獎總人數",contentFormat2));
sheet.addCell(new Label(5, 15,"中獎次數",contentFormat2));
sheet.addCell(new Label(7, 15,"總抽獎次數",contentFormat2));
//交易領獎平臺
//新增邊框
for (int j = 24; j < 31; j++) {
for (int i = 0; i < 14; i++) {
sheet.addCell(new Label(i,j,"",contentFormat2));
}
}
//合併單元格
sheet.mergeCells(0, 23, 13, 23);
sheet.mergeCells(0, 25, 0, 27);
sheet.mergeCells(0, 28, 0, 30);
sheet.mergeCells(1, 24, 3, 24);
sheet.mergeCells(1, 25, 3, 27);
sheet.mergeCells(1, 28, 3, 30);
sheet.mergeCells(5, 24, 6, 24);
sheet.mergeCells(5, 25, 6, 25);
sheet.mergeCells(5, 26, 6, 26);
sheet.mergeCells(5, 27, 6, 27);
sheet.mergeCells(5, 28, 6, 28);
sheet.mergeCells(5, 29, 6, 29);
sheet.mergeCells(5, 30, 6, 30);
sheet.mergeCells(7, 24, 13, 24);
sheet.mergeCells(7, 25, 13, 25);
sheet.mergeCells(7, 26, 13, 26);
sheet.mergeCells(7, 27, 13, 27);
sheet.mergeCells(7, 28, 13, 28);
sheet.mergeCells(7, 29, 13, 29);
sheet.mergeCells(7, 30, 13, 30);
sheet.addCell(new Label(0, 23,"交易領獎平臺",contentFormat2));
sheet.addCell(new Label(0, 24,"序號",contentFormat2));
sheet.addCell(new Label(0, 25,"1",contentFormat2));
sheet.addCell(new Label(0, 28,"2",contentFormat2));
sheet.addCell(new Label(0, 24,"渠道",contentFormat2));
sheet.addCell(new Label(5, 24,"領獎筆數",contentFormat2));
sheet.addCell(new Label(7, 24,"領獎人數",contentFormat2));
sheet.addCell(new Label(1, 25,"網銀渠道",contentFormat2));
sheet.addCell(new Label(1, 28,"手機渠道",contentFormat2));
sheet.addCell(new Label(4, 25,"本月",contentFormat2));
sheet.addCell(new Label(4, 26,"本年",contentFormat2));
sheet.addCell(new Label(4, 27,"總計",contentFormat2));
sheet.addCell(new Label(4, 28,"本月",contentFormat2));
sheet.addCell(new Label(4, 29,"本年",contentFormat2));
sheet.addCell(new Label(4, 30,"總計",contentFormat2));
}
}
寫個測試
public static void main(String[] args) throws Exception {
test();
}
@SuppressWarnings({ })
public static void test() throws Exception{
excel();
//設定單元格的樣式
WritableFont contentFont = new WritableFont(WritableFont.createFont("微軟雅黑"), 10, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE);
WritableCellFormat contentFormat2 = new WritableCellFormat(contentFont);
//設定邊框
contentFormat2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN
,jxl.format.Colour.BLACK);
//設定自動換行
contentFormat2.setWrap(true);
//設定水平居中
contentFormat2.setAlignment(jxl.format.Alignment.CENTRE);
sheet.addCell(new Label(2,3,"本月新增",contentFormat2));
Close();
}
public static void Close() throws IOException, WriteException{
//開始執行寫入操作
book.write();
//關閉流
book.close();
}
效果如圖:
建立一個類,專門寫入資料
package com.kk.DButils;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.kk.jxl.ExchangeExcel;
/**
* @author KK
* 積分,抽獎,領獎 表
*/
public class ExchangeData {
static ExchangeExcel exchange = new ExchangeExcel();
static DBData data = new DBData();
static Map<String, String[]> Mymap = new LinkedHashMap<String, String[]>();
static final Logger logger = LoggerFactory.getLogger(ExchangeData.class);
@SuppressWarnings({ "static-access", "unused" })
public static void excelWrite(String Years,String Months ) throws Exception{
exchange.excel();
//設定單元格的樣式
WritableFont contentFont = new WritableFont(WritableFont.createFont("微軟雅黑"), 10, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE);
WritableCellFormat contentFormat2 = new WritableCellFormat(contentFont);
//設定邊框
contentFormat2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN
,jxl.format.Colour.BLACK);
//設定自動換行
contentFormat2.setWrap(true);
//設定水平居中
contentFormat2.setAlignment(jxl.format.Alignment.CENTRE);
//積分本月
String sql = "select `Year`,`Month`,MerchantsNumber,NumberTransactions,AmountMoney from bankandplatform where DepCode = '1000034' and `Year` = '"+Years+"' and `Month` = '"+Months+"' and PlatformName = '2'";
logger.info("sql語句_積分本月:"+sql);
Mymap = data.getData(sql, "mysql");
String AmountMoney = "";
String NumberTransactions = "";
String MerchantsNumber = "";
String Year = "";
String Month = "";
String Channel = "";
for(String[] s : Mymap.values()){
Year = s[0];
Month = s[1];
MerchantsNumber = s[2];
NumberTransactions = s[3];
AmountMoney = s[4];
if(MerchantsNumber.equals("null")){
MerchantsNumber = "0";
}
if(NumberTransactions.equals("null")){
NumberTransactions = "0";
}
if(AmountMoney.equals("null")){
AmountMoney = "0";
}
exchange.sheet.addCell(new Label(3,2,MerchantsNumber,contentFormat2));
exchange.sheet.addCell(new Label(5,2,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,2,AmountMoney,contentFormat2));
}
Mymap.clear();
//積分本年
String sql2 = "select `Year`,`Month`,MerchantsNumber,NumberTransactions,AmountMoney from bankandplatform where DepCode = '1000034' and `Year` = '"+Years+"' and `Month` = '"+"00"+"' and PlatformName = '2'";
logger.info("sql語句_積分本年:"+sql2);
Mymap = data.getData(sql2, "mysql");
for(String[] s : Mymap.values()){
Year = s[0];
Month = s[1];
MerchantsNumber = s[2];
NumberTransactions = s[3];
AmountMoney = s[4];
if(MerchantsNumber.equals("null")){
MerchantsNumber = "0";
}
if(NumberTransactions.equals("null")){
NumberTransactions = "0";
}
if(AmountMoney.equals("null")){
AmountMoney = "0";
}
exchange.sheet.addCell(new Label(3,3,MerchantsNumber,contentFormat2));
exchange.sheet.addCell(new Label(5,3,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,3,AmountMoney,contentFormat2));
}
Mymap.clear();
//積分歷史所有
String sql3 = "select `Year`,`Month`,MerchantsNumber,NumberTransactions,AmountMoney from bankandplatform where DepCode = '1000034' and `Year` = '"+"0000"+"' and `Month` = '"+"00"+"' and PlatformName = '2'";
logger.info("sql語句_積分歷史所有:"+sql3);
Mymap = data.getData(sql3, "mysql");
for(String[] s : Mymap.values()){
Year = s[0];
Month = s[1];
MerchantsNumber = s[2];
NumberTransactions = s[3];
AmountMoney = s[4];
if(MerchantsNumber.equals("null")){
MerchantsNumber = "0";
}
if(NumberTransactions.equals("null")){
NumberTransactions = "0";
}
if(AmountMoney.equals("null")){
AmountMoney = "0";
}
exchange.sheet.addCell(new Label(3,4,MerchantsNumber,contentFormat2));
exchange.sheet.addCell(new Label(5,4,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,4,AmountMoney,contentFormat2));
}
Mymap.clear();
//企業積分本月
String sql4 = "select `Year`,`Month`,MerchantsNumber,NumberTransactions,AmountMoney from bankandplatform where DepCode = '1000035' and `Year` = '"+Years+"' and `Month` = '"+Months+"' and PlatformName = '2' ";
logger.info("sql語句_企業積分本月:"+sql4);
Mymap = data.getData(sql4, "mysql");
for(String[] s : Mymap.values()){
Year = s[0];
Month = s[1];
MerchantsNumber = s[2];
NumberTransactions = s[3];
AmountMoney = s[4];
if(MerchantsNumber.equals("null")){
MerchantsNumber = "0";
}
if(NumberTransactions.equals("null")){
NumberTransactions = "0";
}
if(AmountMoney.equals("null")){
AmountMoney = "0";
}
exchange.sheet.addCell(new Label(3,5,MerchantsNumber,contentFormat2));
exchange.sheet.addCell(new Label(5,5,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,5,AmountMoney,contentFormat2));
}
Mymap.clear();
//企業積分本年
String sql5 = "select `Year`,`Month`,MerchantsNumber,NumberTransactions,AmountMoney from bankandplatform where DepCode = '1000035' and `Year` = '"+Years+"' and `Month` = '"+"00"+"' and PlatformName = '2' ";
logger.info("sql語句_企業積分本年:"+sql5);
Mymap = data.getData(sql5, "mysql");
for(String[] s : Mymap.values()){
Year = s[0];
Month = s[1];
MerchantsNumber = s[2];
NumberTransactions = s[3];
AmountMoney = s[4];
if(MerchantsNumber.equals("null")){
MerchantsNumber = "0";
}
if(NumberTransactions.equals("null")){
NumberTransactions = "0";
}
if(AmountMoney.equals("null")){
AmountMoney = "0";
}
exchange.sheet.addCell(new Label(3,6,MerchantsNumber,contentFormat2));
exchange.sheet.addCell(new Label(5,6,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,6,AmountMoney,contentFormat2));
}
Mymap.clear();
//企業積分歷史所有
String sql6 = "select `Year`,`Month`,MerchantsNumber,NumberTransactions,AmountMoney from bankandplatform where DepCode = '1000035' and `Year` = '"+"0000"+"' and `Month` = '"+"00"+"' and PlatformName = '2' ";
logger.info("sql語句_企業積分歷史所有:"+sql6);
Mymap = data.getData(sql6, "mysql");
for(String[] s : Mymap.values()){
Year = s[0];
Month = s[1];
MerchantsNumber = s[2];
NumberTransactions = s[3];
AmountMoney = s[4];
if(MerchantsNumber.equals("null")){
MerchantsNumber = "0";
}
if(NumberTransactions.equals("null")){
NumberTransactions = "0";
}
if(AmountMoney.equals("null")){
AmountMoney = "0";
}
exchange.sheet.addCell(new Label(3,7,MerchantsNumber,contentFormat2));
exchange.sheet.addCell(new Label(5,7,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,7,AmountMoney,contentFormat2));
}
Mymap.clear();
//領獎平臺
String ljpt_sql = "select * from costchannel where Type = '領獎平臺' and (`Year` = '"+Years+"' and `Month` = '"+Months+"' or `Year` = '"+Years+"' and `Month` = '00' or `Year` = '0000' and `Month` = '00')";
logger.info("sql語句_領獎平臺 :"+ljpt_sql);
Mymap = data.getData(ljpt_sql, "mysql");
for(String[] s : Mymap.values()){
System.out.println("map:"+Mymap.size());
Year = s[1];
Month = s[2];
AmountMoney = s[4];
NumberTransactions = s[5];
Channel = s[6];
if(AmountMoney.equals("null")){
AmountMoney = "0";
}
if(NumberTransactions.equals("null")){
NumberTransactions = "0";
}
int Yearint = Integer.parseInt(Year);
int Montint = Integer.parseInt(Month);
if( Channel.equals("網銀渠道")){
if(Yearint > 0 && Montint > 0){
exchange.sheet.addCell(new Label(5,25,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,25,AmountMoney,contentFormat2));
}
if(Yearint > 0 && Montint == 0){
exchange.sheet.addCell(new Label(5,26,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,26,AmountMoney,contentFormat2));
}
if(Yearint == 0 && Montint == 0){
exchange.sheet.addCell(new Label(5,27,NumberTransactions,contentFormat2));
exchange.sheet.addCell(new Label(7,27,AmountMoney,contentFormat2));
}
}
if( Channel.equals("手機渠道")){
if(Yearint > 0 && Montint > 0){
exchange.sheet.addCell(new Label(5,28,NumberTransactions,contentFormat2));
exchan