- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- 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;
- public class WriteExcel {
- public static void main(String[] args) {
- FileOutputStream fos;
- try {
- fos = new FileOutputStream("d:\\test.xls");//文件要寫的路徑,如存在則覆蓋,不存在則建立
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet s = wb.createSheet();
- wb.setSheetName(0, "first sheet");
- for(int i=0;i<8;i++){
- HSSFRow row = s.createRow(i);
- for(int j=0;j<8;j++){
- HSSFCell cell = row.createCell(j);
- cell.setCellValue(i+","+j);
- }
- }
- wb.write(fos);
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }