POI Excel操作

3 篇文章 0 订阅

简单的操作 附 Maven 配置

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.9</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-excelant</artifactId>
			<version>3.9</version>
		</dependency>



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  

public class ExcelTest {
	
	public static void crateExcel(){
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("new sheet");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("Name");
		
		HSSFCell cell2 = row.createCell(1);
		cell2.setCellValue("Neal");
		
		HSSFCell cell3 = row.createCell(2);
		cell3.setCellValue(true);
		
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
		HSSFCell dCell = row.createCell(3);
		dCell.setCellValue(new Date());
		dCell.setCellStyle(cellStyle);
		
		HSSFCellStyle backageStyle = wb.createCellStyle();
		backageStyle.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
		HSSFCell cell5 = row.createCell(4);
		cell5.setCellValue("Color");
		cell5.setCellStyle(backageStyle);
		
		HSSFCellStyle backageStyle2 = wb.createCellStyle();
		backageStyle2.setFillForegroundColor(new HSSFColor.GREY_40_PERCENT().getIndex());
		backageStyle2.setBorderBottom((short)1);
		backageStyle2.setBorderTop((short)1);
		backageStyle2.setBorderLeft((short)1);
		backageStyle2.setBorderRight((short)1);
		
		HSSFCell cell6 = row.createCell(5);
		cell6.setCellValue("Color2");
		cell6.setCellStyle(backageStyle2);
		
		// Number Formart
		HSSFCellStyle numberFormart = wb.createCellStyle();
		numberFormart.setDataFormat(HSSFDataFormat.getBuiltinFormat(",###"));
		HSSFCell cell7 = row.createCell(6);
		cell7.setCellValue((double)1111111111.11111);
		cell7.setCellStyle(numberFormart);
		
		
		try {
			FileOutputStream fileOutputStream = new FileOutputStream("workbooks.xls");
			wb.write(fileOutputStream);
			fileOutputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void loadExcelByXsl(){
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("workbooks.xls"));
			HSSFSheet sheet = workbook.getSheet("new sheet");
			HSSFRow row1 = sheet.getRow(0);
			if(row1!=null){
				System.out.println(row1.getCell(0).getStringCellValue());
			}
			HSSFRow row2 = sheet.getRow(1);
			if(row2 == null){
				System.out.println("Null");
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void loadExcelByXslx(){
		try {
			XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("workbooks.xlsx"));
			XSSFSheet sheet = workbook.getSheet("new sheet");
			XSSFRow row1 = sheet.getRow(0);
			if(row1!=null){
				System.out.println(row1.getCell(0).getStringCellValue());
			}
			XSSFRow row2 = sheet.getRow(1);
			if(row2 == null){
				System.out.println("Null");
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		loadExcelByXslx();
	}
	
}


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值