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