Package com. newbee. brooder; <br/> import java. io. file; <br/> import java. io. fileInputStream; <br/> import java. io. fileNotFoundException; <br/> import java. io. fileOutputStream; <br/> import java. io. IOException; <br/> import java. util. arrayList; <br/> import java. util. arrays; <br/> import org. apache. log4j. logger; <br/> import org. apache. poi. hssf. usermodel. HSSFCell; <br/> import org. apache. poi. hssf. usermodel. HS SFCellStyle; <br/> import org. apache. poi. hssf. usermodel. HSSFFont; <br/> import org. apache. poi. hssf. usermodel. HSSFRichTextString; <br/> import org. apache. poi. hssf. usermodel. HSSFRow; <br/> import org. apache. poi. hssf. usermodel. HSSFSheet; <br/> import org. apache. poi. hssf. usermodel. HSSFWorkbook; <br/> public class ReadOrWriteXLS {<br/>/** <br/> * @ param args <br/> */<br/> public static void main (String [] ar Gs) {<br/> writeToXLS (); <br/> readFromXLS (); <br/>}< br/> private static void readFromXLS () {</p> <p> FileInputStream inputStream = null; <br/> try {<br/> inputStream = new FileInputStream (System. getProperty ("user. home ") + File. separator + "test.xls"); <br/> HSSFWorkbook workbook = new HSSFWorkbook (inputStream); <br/> HSSFSheet sheet = workbook. getSheetAt (0); <br/> int rows = sheet. getPhysicalNumberOfRow S (); // number of rows </p> <p> for (int I = 0; I <rows; I ++) {</p> <p> HSSFRow row = sheet. getRow (I); <br/> if (row! = Null) {<br/> int cells = row. getLastCellNum (); <br/> String [] value = new String [cells]; <br/> for (int j = 0; j <cells; j ++) {<br/> HSSFCell cell = row. getCell (j); // cell <br/> if (cell! = Null) {<br/> switch (cell. getCellType () {<br/> case HSSFCell. CELL_TYPE_FORMULA: <br/> break; <br/> case HSSFCell. CELL_TYPE_NUMERIC: <br/> value [j] + = (long) cell. getNumericCellValue () + "/t"; <br/> break; <br/> case HSSFCell. CELL_TYPE_STRING: <br/> value [j] + = cell. getStringCellValue () + "/t"; <br/> break; <br/> default: <br/> value [j] + = "/t "; <br/>}< br/> if (value [j]! = Null) {<br/> value [j] = value [j]. substring (4 ). trim (); // the first four characters are null <br/> if (value [j]. indexOf ("")> 0) {<br/> value [j] = value [j]. replaceAll ("", ""); <br/>}< br/> System. out. println (Arrays. toString (value); <br/>}</p> <p >}catch (FileNotFoundException e) {<br/> Logger. getLogger (ReadOrWriteXLS. class ). debug (e); <br/>}catch (IOException e) {<br/> Logger. getLogger (ReadOrW RiteXLS. class). debug (e); <br/>} finally {<br/> if (inputStream! = Null) {<br/> try {<br/> inputStream. close (); <br/> inputStream = null; <br/>} catch (Exception e2) {<br/> Logger. getLogger (ReadOrWriteXLS. class ). debug (e2); <br/>}</p> <p >}< br/> private static void writeToXLS () {<br/> ArrayList <String []> content = new ArrayList <String []> (); <br/> String [] s01 = "aa, bb, cc, dd, Chinese test, ff ". split (","); <br/> String [] s02 = "11,22, 33,44, 55,66, 77 ". split (","); <br/> Content. add (s01); <br/> content. add (s02); </p> <p> // create an Excel workbook <br/> HSSFWorkbook workbook = new HSSFWorkbook (); <br/> // create a worksheet in an Excel worksheet named sheet1 by default <br/> HSSFSheet sheet = workbook. createSheet ("0"); <br/> // create a font <br/> HSSFFont font = workbook. createFont (); <br/> // set the font color to Red <br/> font. setColor (HSSFFont. COLOR_NORMAL); <br/> // set the font to bold <br/> font. setBoldweight (HSSFFont. BOLDWEIGHT_BOLD); <br/> // Creation format <br/> HSSFCellStyle cellStyle = workbook. createCellStyle (); <br/> // Add the created font to the format <br/> cellStyle. setFont (font); <br/> int beginRow = 0; <br/> for (int I = 0; I <content. size (); I ++) {<br/> // create a row in the worksheet <br/> HSSFRow row = sheet. createRow (beginRow ++); <br/> int beginCol = 0; <br/> for (int j = 0; j <content. get (I ). length; j ++) {<br/> // create a table in one row <br/> HSSFCell cell = row. createCell (short) beginCo L ++); <br/> if (I = 0) {<br/> // Add the preceding format to a cell, the first line of font is bold <br/> cell. setCellStyle (cellStyle); <br/>}< br/> // set the cell to be saved as a string <br/> cell. setCellType (HSSFCell. CELL_TYPE_STRING); <br/> // sets the encoding. This is used to handle Chinese problems. <br/> cell. setEncoding (HSSFCell. ENCODING_UTF_16); <br/> // Add a value to the cell. <br/> cell. setCellValue (new HSSFRichTextString (content. get (I) [j]); <br/>}</p> <p> FileOutputStream outputStream = null; <br/> try {< Br/> outputStream = new FileOutputStream (System. getProperty ("user. home ") + File. separator + "test.xls"); <br/> workbook. write (outputStream); <br/> outputStream. flush (); <br/> outputStream. close (); <br/>}catch (FileNotFoundException e) {<br/> Logger. getLogger (ReadOrWriteXLS. class ). debug (e); <br/>}catch (IOException e) {<br/> Logger. getLogger (ReadOrWriteXLS. class ). debug (e); <br/>}finally {<br/> if (outp UtStream! = Null) {<br/> try {<br/> outputStream. close (); <br/> outputStream = null; <br/>} catch (IOException e) {<br/> Logger. getLogger (ReadOrWriteXLS. class ). debug (e); <br/>}< br/>