poi导出Excel的一些东西_JAVA_编程开发_程序员俱乐部

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

poi导出Excel的一些东西

 2014/10/11 0:38:27  rlox  程序员俱乐部  我要评论(0)
  • 摘要:直接上传代码第一个是多个sheet页packagecom.sjy.poi;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel
  • 标签:excel 导出excel
直接上传代码
第一个是多个sheet页
class="java" name="code">
package com.sjy.poi;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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.poifs.filesystem.POIFSFileSystem;

public class TestExportExcelSheets {

public static void main(String[] args) throws IOException {
		FileInputStream fileinput = new FileInputStream("D:\\datarevised1.xls");
		POIFSFileSystem fs = new POIFSFileSystem(fileinput);
		HSSFWorkbook wbinput = new HSSFWorkbook(fs);
		// --------------------------------------------------------------
		fileinput.close();
		HSSFWorkbook wboutput = new HSSFWorkbook();
		for (int m = 0; m <= 20; m++) {
			
			HSSFSheet sheetoutput = wboutput.createSheet();
			wboutput.setSheetName(m, "Datarude" + String.valueOf(m));

			HSSFSheet sheetinput = wbinput.getSheetAt(0);

			HSSFRow rowinput;
			String cell;// String cell;
			// 循环输出表格中的内容
			for (int i = sheetinput.getFirstRowNum(); i < sheetinput.getPhysicalNumberOfRows(); i++) {
				//基本读写操作				}

			}

			FileOutputStream fileoutput = new FileOutputStream("D:\\new1.xls");
			wboutput.write(fileoutput);
			fileoutput.close();
		}
	}


}


第二个是导出excel
package com.sjy.poi;

import java.io.FileOutputStream;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
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.ss.util.CellRangeAddress;
public class TestExportExcel {



	public static void main(String[] args) throws Exception {
	    // 创建Excel的工作书册 Workbook,对应到一个excel文档
	    HSSFWorkbook wb = new HSSFWorkbook();

	    // 创建Excel的工作sheet,对应到一个excel文档的tab
	    HSSFSheet sheet = wb.createSheet("sheet1");

	    // 设置excel每列宽度
	    sheet.setColumnWidth(0, 4000);
	    sheet.setColumnWidth(1, 3500);

	    // 创建字体样式
	    HSSFFont font = wb.createFont();
	    font.setFontName("Verdana");
	    font.setBoldweight((short) 100);
	    font.setFontHeight((short) 300);
	    font.setColor(HSSFColor.BLUE.index);

	    // 创建单元格样式
	    HSSFCellStyle style = wb.createCellStyle();
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
	    style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
	    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

	    // 设置边框
	    style.setBottomBorderColor(HSSFColor.RED.index);
	    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
	    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
	    style.setBorderTop(HSSFCellStyle.BORDER_THIN);

	    style.setFont(font);// 设置字体

	    // 创建Excel的sheet的一行
	    HSSFRow row = sheet.createRow(0);
	    row.setHeight((short) 500);// 设定行的高度
	    // 创建一个Excel的单元格
	    HSSFCell cell = row.createCell(0);

	    // 合并单元格(startRow,endRow,startColumn,endColumn)
	    //sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));

	    // 给Excel的单元格设置样式和赋值
	    cell.setCellStyle(style);
	    cell.setCellValue("hello world");

	    // 设置单元格内容格式
	    HSSFCellStyle style1 = wb.createCellStyle();
	    style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));

	    style1.setWrapText(true);// 自动换行

	    row = sheet.createRow(1);

	    // 设置单元格的样式格式

	    cell = row.createCell(0);
	    cell.setCellStyle(style1);
	    cell.setCellValue(new Date());

	    // 创建超链接
	    HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
	    link.setAddress("http://www.baidu.com");
	    cell = row.createCell(1);
	    cell.setCellValue("百度");
	    cell.setHyperlink(link);// 设定单元格的链接

	    FileOutputStream os = new FileOutputStream("D:\\workbook.xls");
	    wb.write(os);
	    os.close();
	}





}


第三个是带批注信息的
package com.sjy.poi;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class TestExportExcelComment {

	public static void main(String[] args) throws IOException {
		// 创建工作簿对象
		HSSFWorkbook wb = new HSSFWorkbook();
		// 创建工作表对象
		HSSFSheet sheet = wb.createSheet("我的工作表");
		// 创建绘图对象
		HSSFPatriarch p = sheet.createDrawingPatriarch();
		// 创建单元格对象,批注插入到4行,1列,B5单元格
		HSSFCell cell = sheet.createRow(0).createCell(0);
		// 插入单元格内容
		cell.setCellValue(new HSSFRichTextString("批注"));
		// 获取批注对象
		// (int dx1, int dy1, int dx2, int dy2, short col1, int row1, short
		// col2, int row2)
		// 前四个参数是坐标点,后四个参数是编辑和显示批注时的大小.
		HSSFComment comment = p.createComment(new HSSFClientAnchor(0, 0, 0, 0,
				(short) 3, 3, (short) 5, 6));
		// 输入批注信息
		comment.setString(new HSSFRichTextString("插件批注成功!插件批注成功!"));
		// 添加作者,选中B5单元格,看状态栏
//		comment.setAuthor("toad");
		// 将批注添加到单元格对象中
		cell.setCellComment(comment);
		// 创建输出流
		FileOutputStream out = new FileOutputStream("D:\\writerPostil.xls");

		wb.write(out);
		// 关闭流对象
		out.close();
	}

}

发表评论
用户名: 匿名