JAVA-POI implements EXCEL read/write and java-poiexcel read/write
To complete java excel reading and writing, you must first support the JAVA-POI package. Baidu search can find the resources and will not repeat them:
POI-add an EXCEL file and enter the content
Package com. sh. test. poi; import java. io. fileOutputStream; import org. apache. poi. hssf. usermodel. HSSFCell; import org. apache. poi. hssf. usermodel. HSSFCellStyle; import org. apache. poi. hssf. usermodel. HSSFFont; import org. apache. poi. hssf. usermodel. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. HSSFWorkbook; import org. apache. poi. hssf. util. HSSFColor; public class PoiCreateExcel {/** the location where the Excel file is to be stored. Assume that the */public static String outputFile = "D: \ test.xls" is in drive D "; public static void main (String argv []) {try {// create a new Excel workbook HSSFWorkbook workbook = new HSSFWorkbook (); // create a worksheet in the Excel workbook, its name is the default value. // to create a worksheet named "benefit indicator", the statement is: // HSSFSheet sheet = workbook. createSheet ("benefit indicator"); HSSFSheet sheet = workbook. createSheet ("worksheet added through POI"); // create a row (the top row) at index 0 HSSFRow row = sheet. createRow (short) 0); // create a cell (upper left) HSSFCell cell = row at index 0. createCell (short) 0); // defines the cell type as a string. setCellType (HSSFCell. CELL_TYPE_STRING); // set the cell font format HSSFFont font = workbook. createFont (); // create a font. setColor (HSSFFont. COLOR_RED); // set the font color to Red. Note: HSSFFont only has red. For other colors, use HSSFColor. setBoldweight (HSSFFont. BOLDWEIGHT_BOLD); // set the font format to bold font. setFontName (""); // set the font format to hhssfcellstyle cellStyle = workbook. createCellStyle (); // create a cell style cellStyle. setFont (font); // apply the font style to the cell style cell. setCellStyle (cellStyle); // apply the cell style to cells. // enter some cell content in cells. setCellValue ("added value 1"); // you can specify the column width. setColumnWidth (1, 3766); // The first parameter indicates the column id (starting from 0). The second parameter indicates the width value reference //: the width of "2012-08-10" is 2500 // create a row (the second row at the top) at the position of Index 1 HSSFRow row2 = sheet. createRow (short) 1); // create a cell at Index 1 (the second row in the second row at the upper left) HSSFCell cell2 = row2.createCell (short) 1 ); // define cells as the integer type cell2.setCellType (HSSFCell. CELL_TYPE_NUMERIC); // set the cell font format (more settings style method please Baidu, refer to address: http://www.cnblogs.com/zhenmingliu/archive/2012/04/25/2469396.html) HSSFFont font2 = workbook. createFont (); // new font2.setColor (HSSFColor. BLUE. index); // set the font color to blue, HSSFColor format. XXX (color in English ). index (to short type) font2.setBoldweight (HSSFFont. BOLDWEIGHT_NORMAL); // set the font format to bold font2.setItalic (true); // set the font format to italic true to use italic, the default is flase font2.setFontHeightInPoints (short) 16 ); // set the font2.setFontName (" 文 "); // set the font format to HSSFCellStyle cellStyle2 = workbook. createCellStyle (); // create a cell style cellStyle2.setAlignment (HSSFCellStyle. ALIGN_CENTER); // set the cell center attribute cellStyle2.setFont (font2); // apply the font style to the cell style cell2.setCellStyle (cellStyle2 ); // apply the cell style to cells. // enter cell2.setCellValue ("added value 2") in cells. // create an output file stream, FileOutputStream fOut = new FileOutputStream (outputFile ); // Save the corresponding workbook to the workbook. write (fOut); fOut. flush (); // The operation ends. close the file fOut. close (); System. out. println ("file generated successfully");} catch (Exception e) {System. out. println ("file generation failed:" + e );}}}
After successful, the EXCEL content is as follows:
POI-read New EXCEL content
Package com. sh. test. poi; import java. io. fileInputStream; import java. io. inputStream; import org. apache. poi. hssf. usermodel. HSSFCell; import org. apache. poi. hssf. usermodel. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. HSSFWorkbook; storage location of public class PoiReadExcel {/** Excel files. Note that the forward slash */public static String fileToBeRead = "D: \ test.xls"; public static void main (String argv []) {try {InputStream file = new FileInputStream (fileToBeRead); // create a reference to the Excel workbook file HSSFWorkbook workbook = new HSSFWorkbook (new FileInputStream (fileToBeRead )); // create a reference to the worksheet. // Reference by index in this example // in the Excel document, the default index of the first worksheet is 0, HSSFSheet = workbook. getSheetAt (0); // you can also use getSheet ("Sheet1") to reference it by name. // The statement is HSSFSheet = workbook. getSheet ("Sheet1"); // read the HSSFRow row = sheet. getRow (0); HSSFCell cell = row. getCell (short) 0); // output unit content, cell. getStringCellValue () is the value of the unit System. out. println ("the upper left unit is:" + cell. getStringCellValue ();} catch (Exception e) {System. out. println ("read failed:" + e );}}}
Console output content:
Statement: the code resource is from the network. After my own modification, it is sent to the blog Park and cannot be reproduced without my permission!