POI简单读写excel

当然,首先要下载POI的jar包。官网自己去下载吧

 

代码:

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; 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 Temp1 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File resultFile = null; HSSFWorkbook wb = null; FileOutputStream fos = null; HSSFSheet sheet = null; HSSFRow row = null; HSSFCell cell = null; HSSFCellStyle cellType = null; // result file resultFile = new File("D:/result.xls"); // create a workbook wb = new HSSFWorkbook(); // create a cell style cellType = wb.createCellStyle(); cellType.setBorderTop(HSSFCellStyle.BORDER_THIN); cellType.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellType.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellType.setBorderRight(HSSFCellStyle.BORDER_THIN); cellType.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellType.setFillForegroundColor(HSSFColor.GOLD.index); cellType.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // create a sheet named "hello" sheet = wb.createSheet("hello"); // create a row row = sheet.createRow(1); // create a cell cell = row.createCell(1); // set style of cell cell.setCellStyle(cellType); // set value of cell cell.setCellValue("hello world!"); fos = new FileOutputStream(resultFile); // save workbook wb.write(fos); fos.flush(); fos.close(); } }  

 

另一个:

package hudson.plugins.history.ReportConvter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; 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; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class Temp1 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File kekkaFile = null; File kekkaFile1 = null; InputStream input = null; POIFSFileSystem fs = null; HSSFWorkbook wb = null; FileOutputStream fos = null; HSSFSheet sheet = null; HSSFRow row = null; HSSFCell cell = null; HSSFCellStyle TABLE_TITLE_STYLE = null; kekkaFile = new File("1.xls"); kekkaFile1 = new File("333.xls"); input = new FileInputStream(kekkaFile); fs = new POIFSFileSystem(input); wb = new HSSFWorkbook(fs); // cell style TABLE_TITLE_STYLE = wb.createCellStyle(); TABLE_TITLE_STYLE.setBorderTop(HSSFCellStyle.BORDER_THIN); TABLE_TITLE_STYLE.setBorderBottom(HSSFCellStyle.BORDER_THIN); TABLE_TITLE_STYLE.setBorderLeft(HSSFCellStyle.BORDER_THIN); TABLE_TITLE_STYLE.setBorderRight(HSSFCellStyle.BORDER_THIN); TABLE_TITLE_STYLE.setAlignment(HSSFCellStyle.ALIGN_CENTER); TABLE_TITLE_STYLE.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); TABLE_TITLE_STYLE.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // get first sheet of workbook sheet = wb.getSheetAt(0); // get first row row = sheet.getRow(0); // get first col cell = row.getCell(0); // set value of cell cell.setCellValue("ni hao a!"); TABLE_TITLE_STYLE.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); cell.setCellStyle(TABLE_TITLE_STYLE); fos = new FileOutputStream(kekkaFile1); wb.write(fos); fos.flush(); fos.close(); input.close(); } }  

你可能感兴趣的:(String,Excel,File,table,Class,input)