java excel document generated by jxl

2011-08-29  来源:本站原创  分类:Internet  人气:148 

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import sun.security.krb5.internal.crypto.c;

import com.sun.java_cup.internal.internal_error;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormats;
import jxl.write.WritableCell;
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;

public class ExcelReport {

        private static WritableCellFormat wcf_value;// Tabular data style
        private static WritableCellFormat wcf_value_left;
        private static WritableCellFormat wcf_key;// Header style
        private static WritableCellFormat wcf_name_left;// Table styles
        private static WritableCellFormat wcf_name_right;// Table styles
        private static WritableCellFormat wcf_name_center;// Table styles
        private static WritableCellFormat wcf_title;// Page name pattern
        private static WritableCellFormat wcf_percent_float;

        private static final int MAXCOLS=7;// Table name pattern 

        static{
                try {
                        //WritableFont wf_value = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD);
                        WritableFont wf_key = new jxl.write.WritableFont(WritableFont.createFont(" Microsoft yahei "), 10,WritableFont.BOLD);
                        WritableFont wf_value = new jxl.write.WritableFont(WritableFont.createFont(" Microsoft yahei "), 10,WritableFont.NO_BOLD);
                        wcf_value = new WritableCellFormat(wf_value);
                        wcf_value.setAlignment(jxl.format.Alignment.CENTRE);
                        wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
                        wcf_value.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

                        wcf_value_left = new WritableCellFormat(wf_value);
                        wcf_value_left.setAlignment(jxl.format.Alignment.LEFT);
                        wcf_value_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
                wcf_value_left.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
                wcf_value_left.setWrap(true);

                        //WritableFont wf_key = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
                        wcf_key = new WritableCellFormat(wf_key);
                        wcf_key.setAlignment(jxl.format.Alignment.CENTRE);
                        wcf_key.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

                        wcf_name_left = new WritableCellFormat(wf_key);
                        wcf_name_left.setAlignment(Alignment.LEFT);
                        wcf_name_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

                        wcf_name_right = new WritableCellFormat(wf_key);
                        wcf_name_right.setAlignment(Alignment.RIGHT);

                        wcf_name_center = new WritableCellFormat(wf_key);
                        wcf_name_center.setAlignment(Alignment.CENTRE);

                        jxl.write.NumberFormat wf_percent_float = new jxl.write.NumberFormat("0.00");
                        wcf_percent_float= new jxl.write.WritableCellFormat(wf_value,wf_percent_float);
                        wcf_percent_float.setAlignment(jxl.format.Alignment.CENTRE);
                        wcf_percent_float.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
                        wcf_percent_float.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

                        //WritableFont wf_title = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD);
                        WritableFont wf_title = new jxl.write.WritableFont(WritableFont.createFont(" Microsoft yahei "), 14,WritableFont.BOLD);
                        wcf_title = new WritableCellFormat(wf_title);
                        wcf_title.setAlignment(Alignment.CENTRE);

                } catch (WriteException e) {
                        e.printStackTrace();
                }

        }

        // Generate Excel files
        public void genarateExcel(File file)throws Exception{
                WritableWorkbook wb = Workbook.createWorkbook(file);
                WritableSheet ws = wb.createSheet(" Data report ",0);

                int startRowNum=0;// Starting line
                int startColNum=0;// The starting column
                int maxColSize = 7;// The maximum number of columns 

                // Set column width
                ws.setColumnView(0, 18);
                ws.setColumnView(1, 17);
                ws.setColumnView(2, 19);
                ws.setColumnView(3, 14);
                ws.setColumnView(4, 13);
                ws.setColumnView(5, 11);
                ws.setColumnView(6, 18);

                ws.addCell(new Label(startColNum,startRowNum," Student information report ",wcf_title));
                ws.mergeCells(startColNum,startRowNum, startColNum+maxColSize-1,startRowNum);
                startColNum=0;
                startRowNum++;          

                ws.addCell(new Label(startColNum,startRowNum, "2011 Year in April 08 Today ",wcf_name_right));
                ws.mergeCells(startColNum,startRowNum, startColNum+maxColSize-1,startRowNum);
                startColNum=0;
                startRowNum++;
                generateCells(ws,startRowNum++,startColNum,1,MAXCOLS);

                ws.addCell(new Label(startColNum,startRowNum," The name ",wcf_key));
                ws.mergeCells(startColNum, startRowNum, startColNum+1, startRowNum);
                startColNum=startColNum+2;
                ws.addCell(new Label(startColNum,startRowNum," Gender ",wcf_key));
                ws.mergeCells(startColNum, startRowNum, startColNum+1, startRowNum);
                startColNum=startColNum+2;
                ws.addCell(new Label(startColNum,startRowNum," Age ",wcf_key));
                ws.mergeCells(startColNum, startRowNum, startColNum+1, startRowNum);
                startColNum=startColNum+2;
                ws.addCell(new Label(startColNum,startRowNum," Residential address ",wcf_key));

                startRowNum++;
                startColNum=0;

                for(int i=0;i<10;i++){
                        int rowIndx = startRowNum+i;
                        ws.addCell(new Label(startColNum,rowIndx," The name "+i,wcf_value));
                        ws.mergeCells(startColNum, rowIndx, startColNum+1, rowIndx);
                        startColNum=startColNum+2;
                        ws.addCell(new Label(startColNum,rowIndx,(i%2==0)?" Male ":" Female ",wcf_value));
                        ws.mergeCells(startColNum, rowIndx, startColNum+1, rowIndx);
                        startColNum=startColNum+2;
                        ws.addCell(new Number(startColNum,rowIndx,i+15,wcf_value));
                        ws.mergeCells(startColNum, rowIndx, startColNum+1, rowIndx);
                        startColNum=startColNum+2;
                        ws.addCell(new Label(startColNum,rowIndx," Residential address "+i,wcf_value));

                        startColNum=0;
                }

                wb.write();
                wb.close();
        }

        // Generate empty cells
        public static void generateCells(WritableSheet ws,int startRows,int startColNums,int rows,int cols){
                for(int r=0;r<rows;r++){
                        for(int c = 0;c<cols;c++){
                                try {
                                        ws.addCell(new Label(startColNums+c,startRows+r,""));
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }

        public static void main(String[] args) throws Exception {
                File file = new File("D:\\text.xls");
                file.createNewFile();
                new ExcelReport().genarateExcel(file);
        }

}
相关文章
  • java excel document generated by jxl 2011-08-29

    import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.sql.S

  • EXCEL reports generated poi jxl 2010-04-23

    EXCEL reports generated poi jxl 1.jxl generate reports Java code 1. Package excel; 2. 3. Import java.io.FileOutputStream; 4. Import java.io.OutputStream; 5. Import java.text.SimpleDateFormat; 6. Import java.util.Date; 7. 8. Import jxl.Workbook; 9. Im

  • Ask questions about java excel operation (such as jxl.write.WritableWorkbook.write () method) 2010-06-11

    First set down my question: I have written a program is to first create an excel table, then create a working table (Table I), followed by a write data to the worksheet, write a 600 record, and then just created worksheet to create a statement of Wor

  • jxl export excel document 2010-06-03

    Just work on the encounter to turn one excel document, then paste the others. Later, following someone else has time to quickly write their own, as long as people figure out what is their own, and Haha! Look at the code, this function is get the prom

  • java generate EXCEL table (POI vs JXL) 2010-06-04

    java generate EXCEL table (POI vs JXL) java generate excel form, previously only knew POI, Apache's open source Xiangmu that yes, originally was a set of, Including word, Danshiyinwei word of the complexity and the lack of development Ren Yuan, Hao X

  • jxl use cases (Java -> Excel) 2010-07-14

    Prerequisite for running this example : Go online to download a call jxl.jar of jar Package, and then put your project WEB-INF/lib Next . package gzu.lyq.jxl; import java.io.FileOutputStream; import java.io.OutputStream; import java.text.SimpleDateFo

  • java excel operation 2010-04-18

    Using the Windows operating system, a friend of the Excel (spreadsheet) will definitely be familiar, but to use the Java language to manipulate the Excel file is not an easy task. Increasingly popular in Web applications today, to manipulate Excel fi

  • EXCEL file using JAVA operation (JAVA EXCEL API) 2010-04-22

    JAVA EXCEL API Introduction Java Excel is an open source project through which Java developers can read Excel document, create a new Excel file, update an existing Excel file. Non-Windows operating system using the API can also be pure Java applicati

  • java excel table operation 2010-07-05

    About Java Excel JAVA EXCEL API is an open source project through which Java developers can read Excel document, create a new Excel file, update Existing Excel file. Non-Windows operating system using the API can also be pure Java application to hand

  • Full excavation Java Excel API to use 2010-04-22

    Friends use Windows operating system Excel (spreadsheet) will definitely be familiar, but to use the Java language to manipulate the Excel file is not an easy task. Increasingly popular in Web applications today, through the Web to manipulate Excel f

  • Export to excel using jsp Summary (jxl) 2010-05-10

    Export to excel using jsp 200 Summary (jxl) Set excel export format as ms word and excel documents that support the html text format, so you can start with a good word or excel template, Save as Web page, and then change the html jsp, fill in the dat

  • JAVA EXCEL API详解 2012-06-10

    使用Windows操作系统的朋友对Excel(电子表格)一定不会陌生,但是要使用Java语言来操纵Excel文件并不是一件容易的事.在Web应用日益盛行的今天,通过Web来操作Excel文件的需求越来越强烈,目前较为流行的操作是在JSP或Servlet 中创建一个CSV (comma separated values)文件,并将这个文件以MIME,text/csv类型返回给浏览器,接着浏览器调用Excel并且显示CSV文件.这样只是说可以访问到Excel文件,但是还不能真正的操纵Excel文件,

  • Operation with xlrd and xlwt Excel document 2010-06-26

    Work which often operate excel document, and some completely pure duplication. In my work, he would face such a demand: 1. There are two excel documents (source.xls / target.xls), the column data is consistent 2. Documentation source.xls data from ta

  • java excel export data file format is not valid 2010-12-04

    java excel export data file format is invalid because WritableWorkbook workbook=Workbook.createWorkbook(new File("h:\\a.xls")); workbook.createSheet("abc", 0); workbook.write();// Do not call this function to be a problem workbook.clos

  • The Document generated by FlashDeveloper generated API documentation generation tool 2011-05-26

    The Document generated by FlashDeveloper document generation tool to generate API, which is very convenient ... asdoc .... in fact, and the javadoc one thing, to be marked after the memo it ... Reference: http://blog.csdn.net/cjy37/archive/2009/02/18

  • Java using POI generated Excel mandatory wrap 2010-04-15

    Some time ago to do a school in Timetabling system, there is a need to use the curriculum areas generate excel export to the customer, prior to use excel as only a simple application, are used in cell Gerry wrap, and this may need to manually force t

  • java itext rtf document generated 2010-05-08

    Use itext to generate rtf documents, first need to download three packages, namely: iText-2.1.7.jar, iText-rtf-2.1.7.jar and iTextAsian.jar, these packages have been uploaded from "itext Application Package" to download. Online There are many ap

  • Import excel document is error java.io.IOException: Invalid header signature; 2011-04-28

    Error message: java.io.IOException: Invalid header signature; read 4503608217567241, expected -2226271756974174256 Obtained online at the following solution: POI to read Excel file with an error. The usual reason is not read file header information m

  • Java Excel file download 2010-03-29

    import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExcelDownload extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws Servle

  • Commons-POI - Java Excel operations 2010-04-19

    ★ , POI, many components are not able to spend, according to need to select their own needs, and I used here is the SS (HSSF + XSSF), and were for xls and xlsx, generally should be ok to use HSSF to, or it more people with office2003 ★ , most of the