您的位置:首页 > 编程语言 > Java开发

java读写创建excel

2015-06-06 11:03 651 查看
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}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息