Java excel file operations, java excel operations

Source: Internet
Author: User

Java excel file operations, java excel operations

Two java excel operations methods were learned two days ago. You can use these two methods to create, modify, and export excel files for a website. The first method is to use JExcel, the second method is to use HSSF in poi. The former method is relatively simple. Here is a summary.

1. JExcel, the required jar package jxl. jar

Read-Only files

Workbook wb = Workbook. getWorkbook (File path)

Allows you to read and write files and create a new file.

WritableWrokbook wwb = Wrokbook. createWorkbook (String filePath)

The main hierarchies are as follows:

Important methods:

// Set the font style WritableFont font = new WritableFont (WritableFont. TIMES, 15, WritableFont. BOLD, false, UnderlineStyle. NO_UNDERLINE, Colour. BLACK); WritableCellFormat fmt = new WritableCellFormat (font); fmt. setAlignment (Alignment. CENTRE); Label label1 = new Label (0, 0, "Software Engineering first shift list", fmt );
Sheet. mergeCells (0, 0, 4, 0); // merge the sheet. setRowView (0,500) cells; // you can specify the width of the first row.
For (int I = 1; I <rsheet. getRows (); I ++) // cyclically obtain the content of the sheet for (int j = 0; j <rsheet. getColumns (); j ++) {Label label = new Label (j, I, rsheet. getCell (j, I ). getContents (); sheet. addCell (label );}
DateFormat df = new DateFormat ("yyyy-MM-dd"); // display method of the specified time WritableCellFormat wcfDF = new WritableCellFormat (df); DateTime tm = new DateTime (0, rsheet. getRows () + 1, new Date (), wcfDF); sheet. addCell (tm); wbook. write ();
2. The structure of HSSF in POI is similar to that in JExcel.

HSSFWorkbook-(createSheet)> HSSFSheet-(createRow)> HSSFRow-(createCell)> HSSFCell. Unlike JExcel, HSSFWorkbook is imported to the file stream only after it has no objects in the column, locate columns by row.

Important modification methods:

Font. setColor (HSSFFont. COLOR_RED); font. setBoldweight (HSSFFont. BOLDWEIGHT_BOLD); HSSFCellStyle cellStyle = book. createCellStyle (); // Add cellStyle to the style. setDataFormat (book. createDataFormat (). getFormat ("yyyy-MM-dd"); // sets the cellStyle time representation. setFont (font); cell. setCellStyle (cellStyle );

For more methods, refer to the api documentation.

13:23:25

Related Article

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.