java读写创建excel

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadWriteExcel {
	
	public static void readExcel(String fileName) throws FileNotFoundException, IOException, InvalidFormatException {
		Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(fileName)));
		Sheet sheet = workbook.getSheetAt(0);
		List<Map<String, Object>> userList = new ArrayList<Map<String,Object>>();
		for(int i = 1 ; i < sheet.getPhysicalNumberOfRows(); i++) {
			Row row = sheet.getRow(i);
			Map<String, Object> user = new HashMap<String, Object>();
			Cell cell0 = row.getCell(0);	//username
			cell0.setCellType(Cell.CELL_TYPE_STRING);
			
			Cell cell1 = row.getCell(1);	//password
			cell1.setCellType(Cell.CELL_TYPE_STRING);
			
			Cell cell2 = row.getCell(2);	//register_time
			cell2.setCellType(Cell.CELL_TYPE_NUMERIC);
			
			user.put("username", cell0.toString());
			user.put("password", cell1.toString());
			user.put("register_time", cell2.toString());
			userList.add(user);
			System.out.println(user);
		}
	}
	
	public static void createExcel(String filePath) throws IOException {
		XSSFWorkbook workBook = new XSSFWorkbook();	//创建 一个excel文档对象(office 2007)
		//HSSFWorkbook workBook = new HSSFWorkbook();//创建office 2003版本的excel
		XSSFSheet sheet = workBook.createSheet();	//创建一个工作薄对象
		sheet.setColumnWidth(1, 10000);// 设置第二列的宽度
		
		XSSFRow row = sheet.createRow(1);// 创建一个行对象(此处为第2行)
		row.setHeightInPoints(23);// 设置行高23像素
		
		XSSFCellStyle style = workBook.createCellStyle();// 创建样式对象
		// 设置字体
		XSSFFont font = workBook.createFont();// 创建字体对象    
		font.setFontHeightInPoints((short)15);// 设置字体大小    
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体    
		font.setFontName("黑体");// 设置为黑体字    
		style.setFont(font);// 将字体加入到样式对象    
		
		// 设置对齐方式 
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);// 水平居中    
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
		
		// 设置边框 
		style.setBorderTop(HSSFCellStyle.BORDER_THICK);// 顶部边框粗线    
		style.setTopBorderColor(HSSFColor.RED.index);// 设置为红色
		style.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);// 底部边框双线    
		style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);// 左边边框    
		style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);// 右边边框
		
		XSSFCell cell1 = row.createCell(1);// 创建单元格(第二行第二列)
		SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm");
		cell1.setCellValue(format.format(new Date()));// 写入当前日期    
		cell1.setCellStyle(style);// 应用样式对象
		
		XSSFCell cell2 = row.createCell(2);// 创建单元格(第二行第三列)
		cell2.setCellValue("write excel2007 test!");
		
		// 文件输出流 
		FileOutputStream os = new FileOutputStream(filePath);
		workBook.write(os);// 将文档对象写入文件输出流    
		os.close();// 关闭文件输出流 
		System.out.println("创建成功 office 2007 excel");
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
		createExcel("E:/writetest.xlsx");
		readExcel("E:/users.xls");
	}
	
}

writetest.xlsx


users.xls


output:

创建成功 office 2007 excel
{username=张三, register_time=08-八月-2014, password=123456}
{username=李四, register_time=10-一月-2015, password=654321}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值