JXL(JExcleApi)Excle详细使用心得

功能:写入/读取Excle,设置打印样式排版,锁定列与行,设置打印标题,给Excle加密。

java报表JXL和POI打印设置
 =======================================   引用来自互联网  start ===============================================

JXL的打印设置在jxl.SheetSettings这个类中,我们可以通过方法Sheet(或者WritableSheet#getSettings()获取。

1.页面

1.1方向

SheetSetting#setOrientation(PageOrientation po)

参数:  PageOrientation#LANDSCAPE       横向打印

PageOrientation# PORTRAIT       纵向打印

1.2缩放

1.2.1缩放比例(A) SheetSetting #setScaleFactor (int);百分比形式

1.2.2调整(F)

页宽 SheetSetting #setFitWidth(int);

页高 SheetSetting #setFitHeight(int);

1.3纸张大小(Z) SheetSetting #setPaperSize (PaperSize);

纸张大小的定义说明参见PaperSize类中的常量。

1.4起始页码(R) SheetSetting #setPageStrart(int);[默认状态]

2页面距

2.1(T) SheetSetting # setTopMargin (double);

2.2(B) SheetSetting # setBottomMargin (double);

2.3(L) SheetSetting # setLeftMargin (double);

2.4(R) SheetSetting # setRightMargin (double);

2.5页眉(A) SheetSetting #setHeaderMargin(double);

2.6页脚(F) SheetSetting #setFooterMargin(double);

2.7居中方式

2.7.1水平(Z) SheetSetting # setHorizontalCentre (boolean);

2.7.2垂直(V) SheetSetting #setVerticallyCenter(boolean);

3页眉/页脚

3.1页眉SheetSetting # setHeaderHeaderFooter;

说明:

对于HeaderFooter的设置,首先确定页眉的位置(左、中、右),通过HeaderFooter#getCentre()(或者getLeft()getRight())方法获取HeaderFooter.Contents类,之后对这个类的属性进行操作。

下面简单介绍几个常用方法:

设置字号:Contents#setFontSize(int)

设置字体:Contents#setFontName(String)

设置内容:Contents# appendString),还有appendDate()当前日期等等,具体参考Contents类说明

3.2页脚SheetSetting # setFooterHeaderFooter);

说明同上

4工作表

4.1打印区域

SheetSettings #setPrintArea(firstCol, firstRow, lastCol, lastRow)

4.2打印标题

SheetSettings#setPrintTitles (firstRow,lastRow,firstCol,lastCol);

SheetSettings#setPrintTitlesCol(firstCol,lastCol)

SheetSettings#setPrintTitlesRow(firstRow,lastRow)
 =======================================   引用来自互联网  end ===============================================

=======================================  自己写的Demo ========================================
DEMO

 

package finance.barcode;

import static jxl.format.PaperSize.getPaperSize;

import include.nseer_db.nseer_db;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import jxl.CellView;
import jxl.HeaderFooter;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.PageOrientation;
import jxl.format.PaperSize;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * 
 * 
@author Solitary
 * 
@author xing5156@gmail.com
 *
 
*/

public class BarToExcle extends nseer_db{

    
/** *********创建字体**************** */
    
public WritableFont BlodFont ;             // 粗体
    public WritableFont NormalFont ;        // 普通字体
    public WritableFont BarCodeFont ;         // 条形码字体
    /** ************以下设置几种格式的单元格属性************ */
    
public WritableCellFormat wcf_header ;    // 标题样式
    public WritableCellFormat wcf_left ;    // 用于正文左
    public WritableCellFormat wcf_barcode ; // 用于条形码格式
    
    
    
public BarToExcle(String db) {        
        
super(db) ;
        
        
/** ********* 初始化字体 **************** */
        BlodFont 
= new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD ) ;
        NormalFont 
= new WritableFont(WritableFont.ARIAL, 15);
        BarCodeFont 
= new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD);
        
        
try {
            
/** ********* 初始化单元格属性 **************** */
            
// 初始化标题样式
            wcf_header = new WritableCellFormat(BlodFont, NumberFormats.TEXT) ; // 实例化单元格格式对象(标题、居中)
            wcf_header.setBorder(Border.ALL, BorderLineStyle.THIN) ;    // 线条
            wcf_header.setVerticalAlignment(VerticalAlignment.CENTRE);     // 垂直对齐
            wcf_header.setAlignment(Alignment.CENTRE);                     // 水平对齐
            wcf_header.setBackground(Colour.GRAY_25) ;                    // 背景颜色
            wcf_header.setWrap(true);                                     // 是否换行
            
            
// 用于正文左
            wcf_left = new WritableCellFormat(NormalFont);                // 实例化单元格格式对象(正文、左对齐)
            wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN);         // 线条
            wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);     // 垂直对齐
            wcf_left.setAlignment(Alignment.LEFT);
            wcf_left.setLocked(
false) ;                                    // 设置锁定,还得设置SheetSettings启用保护和设置密码
            wcf_left.setWrap(true);                                     // 是否换行
            
            
// 用于条形码格式
            wcf_barcode = new WritableCellFormat(BarCodeFont);            // 实例化单元格格式对象(第一列、条形码)
            wcf_barcode.setBorder(Border.ALL, BorderLineStyle.THIN);     // 线条
            wcf_barcode.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
            wcf_barcode.setAlignment(Alignment.CENTRE);
            wcf_barcode.setLocked(
true) ;                                // 设置锁定,还得设置SheetSettings启用保护和设置密码
            wcf_barcode.setWrap(true);                                     // 是否换行
        }
 catch (WriteException e) {
            e.printStackTrace();
        }

    }

    
    
    
// 创建Excle及工作表
    public int createExcle(StringBuffer buf) {
        String barCodes[] 
= null ;
        
try {
            
/** ********** 创建工作薄  ********** **/
            WritableWorkbook workbook 
= Workbook.createWorkbook(new File("d:" + File.separator + "barcode.xls")) ;
            
/** ********** 创建工作表  ********** **/
            WritableSheet sheet 
= workbook.createSheet("条形码"0) ;
            
            
/** ********* 打印属性 **************** */
            sheet.getSettings().setOrientation(PageOrientation.LANDSCAPE) ;    
// 设置为横向打印
            sheet.getSettings().setPaperSize(PaperSize.A4) ;            // 设置纸张
            sheet.getSettings().setFitHeight(297) ;                        // 打印区高度
            sheet.getSettings().setFitWidth(210) ;                        // 打印区宽度    
            
// 设置列宽
            sheet.setColumnView(010); // 第1列
            sheet.setColumnView(130); // 第2列
            sheet.setColumnView(215);
            sheet.setColumnView(
315);
            sheet.setColumnView(
430);
            sheet.setColumnView(
540);
            
// 设置边距            
            sheet.getSettings().setTopMargin(0.5) ;
            sheet.getSettings().setBottomMargin(
0.3) ;
            sheet.getSettings().setLeftMargin(
0.1) ;
            sheet.getSettings().setRightMargin(
0.1) ;
            
// 设置页脚
            sheet.getSettings().getFooter().getCentre().appendPageNumber() ;    // 为页脚添加页数
            sheet.getSettings().setFooterMargin(0.07) ;                            // 设置页脚边距(下)
            
// 设置保护,并加密码 锁定的Cell才会起作用
            sheet.getSettings().setProtected(true) ;                            // 启用保护
            sheet.getSettings().setPassword("xing5156") ;                        // 设置保护密码
            
// 设置打印标题行
            sheet.getSettings().setPrintHeaders(true) ;                            // 启用打印头信息
            sheet.getSettings().setPrintTitlesRow(00) ;                        // 设置标题行
            
            
/** *********插入标题内容**************** */
            sheet.addCell(
new Label(00"条码"this.wcf_header));
            sheet.addCell(
new Label(10"公司"this.wcf_header));
            sheet.addCell(
new Label(20"部门"this.wcf_header));
            sheet.addCell(
new Label(30"姓名"this.wcf_header));
            sheet.addCell(
new Label(40"类型"this.wcf_header));
            sheet.addCell(
new Label(50"描述"this.wcf_header));

            barCodes 
= buf.toString().split("") ;
            
for(int i = 0, y = 1; i < barCodes.length; i++, y++{
                sheet.setRowView(y, 
400) ;
                sheet.addCell(
new Label(0, y, barCodes[i], this.wcf_barcode)) ;
                sheet.addCell(
new Label(1, y, ""this.wcf_left)) ;
                sheet.addCell(
new Label(2, y, ""this.wcf_left)) ;
                sheet.addCell(
new Label(3, y, ""this.wcf_left)) ;
                sheet.addCell(
new Label(4, y, ""this.wcf_left)) ;
                sheet.addCell(
new Label(5, y, ""this.wcf_left)) ;
            }

            
            
/** **********以上所写的内容都是写在缓存中的,下一句将缓存的内容写到文件中******** */
            workbook.write();
            
/** *********关闭文件************* */
            workbook.close();
            System.out.println(
"导出成功");
            
        }
 catch (Exception e) {
            System.out.println(
"在输出到EXCEL的过程中出现错误,错误原因:" + e.toString());
        }

        
return barCodes.length ;
    }

    
/**
     * 构建Workbook对象, 将内容读取到内存中再更新到原来文件中 
     * 最后关闭写入操作来完成文件更新
     * 
@param buf 该对象中包含(11AA◎55BC◎523A)类似的数据,以◎拆分
     * 
@return    返回本次更新条数
     
*/

    
public int updateExcle(StringBuffer buf) throws Exception {            // 更新Excle
        
        File file 
= new File("d:" + File.separator + "barcode.xls") ;
        Workbook wb 
= null;
        
if(file.exists()) {
            wb 
= Workbook.getWorkbook(file);
            WritableWorkbook wwb 
= Workbook.createWorkbook(file, wb);
            
// 读取第一张工作表
            WritableSheet sheet = wwb.getSheet(0) ;
            
            
int count = sheet.getRows() ;
            String barCodes[] 
= buf.toString().split("") ;
            
for(int i = count , y = 0; y < barCodes.length; y++ , i++{
                sheet.setRowView(i, 
400) ;
                sheet.addCell(
new Label(0, i, barCodes[y], this.wcf_barcode)) ;
                sheet.addCell(
new Label(1, i, ""this.wcf_left)) ;
                sheet.addCell(
new Label(2, i, ""this.wcf_left)) ;
                sheet.addCell(
new Label(3, i, ""this.wcf_left)) ;
                sheet.addCell(
new Label(4, i, ""this.wcf_left)) ;
                sheet.addCell(
new Label(5, i, ""this.wcf_left)) ;
            }

            wwb.write() ;
            wwb.close() ;
            wb.close() ;
            System.out.println(
"更新完成!") ;
            
return barCodes.length ;
        }
 else {
            
return createExcle(buf) ;
        }

    }

    
    
// 测试
//    public static void main(String args[]) {
//        StringBuffer buf = new StringBuffer() ;        // 测试数据
//        buf.append("56KF").append("◎").append("12AA").append("◎").append("23GH").append("◎").append("55JJ") ;
//        try {
//            BarToExcle bte = new BarToExcle() ;
//            bte.updateExcle(buf) ;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}


posted on 2011-11-18 14:04 Solitary 阅读(27973) 评论(1)  编辑  收藏 所属分类: Tool

评论

# re: JXL(JExcleApi)Excle详细使用心得 2015-11-13 10:02 周周周周

include是自己洗的么、  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜