java利用poi导出excel_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java利用poi导出excel

java利用poi导出excel

 2013/11/6 22:10:51  Mrzhao2013  程序员俱乐部  我要评论(0)
  • 摘要:packagezy.demo.test;importorg.apache.poi.hssf.usermodel.HSSFFont;importorg.apache.poi.hssf.usermodel.HSSFRichTextString;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf
  • 标签:excel 导出excel Java 利用
class="java" name="code">
package zy.demo.test;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
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.util.Region;
import org.hibernate.mapping.Array;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ToExcel {
	public static String xlsFile = "d:/test.xls"; // 产生的Excel文件的名称

	// 设置cell编码解决中文高位字节截断
	@SuppressWarnings("deprecation")
	public static void main(String[] agrs) throws IOException {
		HSSFWorkbook workbook = new HSSFWorkbook(); // 产生工作簿对象
		HSSFSheet sheet = workbook.createSheet("工作表对象"); // 产生工作表对象
		// 设置第一个工作表的名称为firstSheet
		// 为了工作表能支持中文,设置字符编码为UTF_16
		workbook.setSheetName(0, "firstSheet");
		// 产生一行,再加8行
		HSSFRow row = sheet.createRow((short) 0);
		HSSFRow row1 = sheet.createRow((short) 1);
		// 设置每一列的宽度
		sheet.setColumnWidth((short) 0, (short) 4000);
		sheet.setColumnWidth((short) 1, (short) 4000);
		sheet.setColumnWidth((short) 2, (short) 4000);
		sheet.setColumnWidth((short) 3, (short) 4000);
		sheet.setColumnWidth((short) 4, (short) 4000);
		sheet.setColumnWidth((short) 5, (short) 4000);
		sheet.setColumnWidth((short) 6, (short) 5000);
		sheet.setColumnWidth((short) 7, (short) 5000);
		sheet.setColumnWidth((short) 8, (short) 5000);
		// 产生列
		HSSFCell cell1 = row1.createCell((short) 0);
		// 设置单元格内容为字符串型
		cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
		// 为了能在单元格中写入中文,设置字符编码为UTF_16。.....

		// 往第一个单元格中写入信息
		cell1.setCellValue("第二行");
		String[] head = { "行号", "头1", "头2", "头3", "头4", "头5", "头6", "头7", "头8" };
		for (int i = 0; i < head.length; i++) {
			HSSFCell cell = row.createCell((short) i);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue(head[i]);
		}
		ArrayList<String> list = new ArrayList();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		for(int i=0;i<list.size();i++){
			//创建行
			HSSFRow row2 = sheet.createRow((short)(i+2));
			//创建列
			HSSFCell cell = row2.createCell((short)0);
			cell.setCellValue(list.get(0));
		}
		FileOutputStream fOut = new FileOutputStream(xlsFile);
		;
		try {
			fOut = new FileOutputStream(xlsFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		workbook.write(fOut);
		fOut.flush();
		System.out.println("文件生成...");
		// 以下语句读取生成的Excel文件内容
		FileInputStream fIn = new FileInputStream(xlsFile);
		HSSFWorkbook readWorkBook = new HSSFWorkbook(fIn);
		HSSFSheet readSheet = readWorkBook.getSheet("firstSheet");
		HSSFRow readRow = readSheet.getRow(0);
		HSSFCell readCell = readRow.getCell((short) 0);
		HSSFCell readCell1 = readRow.getCell((short) 1);
		System.out.println("第一个单元是:" + readCell.getStringCellValue());
		System.out.println(readCell1.getStringCellValue());
	}
}

发表评论
用户名: 匿名