POI简单读写excel时间:2021-10-20 20:24:51当然,首先要下载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 fileresultFile = new File("D:/result.xls");// create a workbookwb = new HSSFWorkbook();// create a cell stylecellType = 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 rowrow = sheet.createRow(1);// create a cellcell = row.createCell(1);// set style of cellcell.setCellStyle(cellType);// set value of cellcell.setCellValue("hello world!");fos = new FileOutputStream(resultFile);// save workbookwb.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 styleTABLE_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 workbooksheet = wb.getSheetAt(0);// get first rowrow = sheet.getRow(0);// get first colcell = row.getCell(0);// set value of cellcell.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();}}