Java操作Excel之Poi

package com.java1234.poi;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo1 {

    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        FileOutputStream fileOut=new FileOutputStream("c:\\用Poi搞出来的工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}
package com.java1234.poi;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo2 {

    public static void main(String[] args) throws Exception {
        
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        wb.createSheet("第二个Sheet页");  // 创建第二个Sheet页
        FileOutputStream fileOut=new FileOutputStream("c:\\用Poi搞出来的Sheet页.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}
package com.java1234.poi;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo3 {

    public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(0); // 创建一个行
        Cell cell=row.createCell(0); // 创建一个单元格  第1列
        cell.setCellValue(1);  // 给单元格设置值
        
        row.createCell(1).setCellValue(1.2);   // 创建一个单元格 第2列 值是1.2
        
        row.createCell(2).setCellValue("这是一个字符串类型"); // 创建一个单元格 第3列 值为一个字符串
        
        row.createCell(3).setCellValue(false);  // 创建一个单元格 第4列 值为布尔类型
        
        FileOutputStream fileOut=new FileOutputStream("c:\\用Poi搞出来的Cell.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}
package com.java1234.poi;

import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo4 {

    public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(0); // 创建一个行
        Cell cell=row.createCell(0); // 创建一个单元格  第1列
        cell.setCellValue(new Date());  // 给单元格设置值
        
        CreationHelper createHelper=wb.getCreationHelper();
        CellStyle cellStyle=wb.createCellStyle(); //单元格样式类
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyy-mm-dd hh:mm:ss"));
        cell=row.createCell(1); // 第二列
        cell.setCellValue(new Date());
        cell.setCellStyle(cellStyle);
        
        cell=row.createCell(2);  // 第三列
        cell.setCellValue(Calendar.getInstance());
        cell.setCellStyle(cellStyle);
        
        FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

jar包下载     https://download.csdn.net/download/qq_38327551/10558375

猜你喜欢

转载自www.cnblogs.com/duanwandao/p/9356668.html