Excel operations in java

Source: Internet
Author: User

To use java to Operate Excel, You need to download an Open Source jar package-jxl. jar. I. read Excel file Content java code/** read Excel File content * @ param file File to be read * @ return */public static String readExcel (file File) {StringBuffer sb = new StringBuffer (); Workbook wb = null; try {// construct the Workbook (working thin) object wb = Workbook. getWorkbook (file);} catch (BiffException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} if (wb = null) return null; // after obtaining the Workbook object, you can use it to obtain the Sheet (worksheet) object. Sheet [] she Et = wb. getSheets (); if (sheet! = Null & sheet. length> 0) {// cycle each worksheet for (int I = 0; I <sheet. length; I ++) {// get the number of rows in the current worksheet int rowNum = sheet [I]. getRows (); for (int j = 1; j <rowNum; j ++) {// obtain all cells in the current row. Cell [] cells = sheet [I]. getRow (j); if (cells! = Null & cells. length> 0) {// cycle each cell for (int k = 0; k // read the value of the current cell String cellValue = cells [k]. getContents (); sb. append (cellValue + "\ t") ;}} sb. append ("\ r \ n");} sb. append ("\ r \ n") ;}// closes the resource and releases the memory wb. close (); return sb. toString ();} 2. write Excel file here there are a lot of formats, such as text content bold, plus some color, can refer to the jxl api, but also recommend a good article: http://www.ibm.com/developerworks/cn/java/l-javaExcel? Ca = j-t10 java code/** generate an Excel file * @ param fileName the Excel file name to be generated */public static void writeExcel (String fileName) {WritableWorkbook wwb = null; try {// first use the Workbook class factory method to create a writeable Workbook object wwb = Workbook. createWorkbook (new File (fileName);} catch (IOException e) {e. printStackTrace ();} if (wwb! = Null) {// create a writable worksheet // The createSheet method of the Workbook has two parameters. The first one is the worksheet name, the second is the position of the worksheet in the workbook WritableSheet ws = wwb. createSheet ("sheet1", 0); // Add the cell for (int I = 0; I <10; I ++) {for (int j = 0; j <5; j ++) {// note that in Excel, the first parameter table represents the column, and the second indicates the row Label labelC = new Label (j, I, "This is the" + (I + 1) + "Row, the" + (j + 1) + "column "); try {// Add the generated cells to the worksheet ws. addCell (labelC);} catch (RowsExceededException e) {e. printStackTrace ();} catch (writeiterator t Ion e) {e. printStackTrace () ;}}try {// write wwb to the file from the memory. write (); // close the resource and release the memory wwb. close ();} catch (IOException e) {e. printStackTrace ();} catch (WriteException e) {e. printStackTrace () ;}} 3. search for a keyWord in an Excel file in java code/** search for a file that contains a keyWord * @ param file to be searched * @ param keyWord keyword * @ return */public static boolean searchKeyWord (File file, string keyWord) {boolean res = false; Workbook Wb = null; try {// construct the Workbook (working thin) object wb = Workbook. getWorkbook (file);} catch (BiffException e) {return res;} catch (IOException e) {return res;} if (wb = null) return res; // after obtaining the Workbook object, you can use it to obtain the Sheet (worksheet) object (Sheet [] sheet = wb. getSheets (); boolean breakSheet = false; if (sheet! = Null & sheet. length> 0) {// cycle each worksheet for (int I = 0; I if (breakSheet) break; // obtain the number of rows in the current worksheet. int rowNum = sheet [I]. getRows (); boolean breakRow = false; for (int j = 0; j if (breakRow) break; // obtain all cells in the current row. Cell [] cells = sheet [I]. getRow (j); if (cells! = Null & cells. length> 0) {boolean breakCell = false; // cyclically for (int k = 0; k if (breakCell) break for each cell; // read the current cell value String cellValue = cells [k]. getContents (); if (cellValue = null) continue; if (cellValue. contains (keyWord) {res = true; breakCell = true; breakRow = true; breakSheet = true ;}}}}// finally closes the resource and releases the memory wb. close (); return res;} 4. it is easy to insert an image icon into an Excel file. See the following code: java code/** insert an image to Excel * @ param dataSheet worksheet to be inserted * @ param col image from this column * @ param row image from this row * @ param width Image number of columns occupied * @ param height the number of rows occupied by the image * @ param imgFile the image file to be inserted */public static void insertImg (WritableSheet dataSheet, int col, int row, int width, int height, File imgFile) {WritableImage img = new WritableImage (col, row, width, height, imgFile); dataSheet. addImage (img);} the comments of the above Code are clear, so we may not need to explain it again. We can use the following program for verification: java code try {// create a workbook WritableWorkbook Workbook = workbook. createWorkbook (new File ("D:/test1.xls"); // WritableSheet imgSheet = workbook. createSheet ("Images", 0); // File imgFile = new File ("D:/1.png"); // Insert the image to the first cell of the second row, insertImg (imgSheet, imgFile); workbook. write (); workbook. close (); catch (IOException e) {e. printStackTrace (); catch (WriteException e) {e. printStackTrace (); however, jxl only supports images in png format. Both jpg and gif formats do not support five. insert the header and footer. Generally, the header and footer are divided into three parts: Left, center, and right, use the following code to insert a header and footer: java code/** Add a header and footer to Excel * @ param dataSheet worksheet to add a header * @ param left * @ param center * @ param right */public static void setHeader (WritableSheet dataSheet, string left, String center, String right) {HeaderFooter hf = new HeaderFooter (); hf. getLeft (). append (left); hf. getCentre (). append (center); hf. getRight (). append (right); // Add the header dataSheet. getSettings (). setHeader (hf); // Add the footer // dataSheet. getSettings (). setFooter (hf);} we can use the following code to test this method: java code try {// create a working thin WritableWorkbook workbook = Workbook. createWorkbook (new File ("D:/test1.xls"); // The Worksheet to be inserted WritableSheet dataSheet = workbook. createSheet ("add Header", 0); ExcelUtils. setHeader (dataSheet, "chb", "", "1st pages, 3 pages in total"); workbook. write (); workbook. close ();} catch (IOException e) {e. printStackTrace ();} catch (WriteException e) {e. printStackTrace ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.