⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 excelsample3.java

📁 此源码程序实现的是eclipse开发工具中插件的开发实现参考程序
💻 JAVA
字号:
package cn.com.chengang.myplugin.poi;

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

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;

public class ExcelSample3 {
	public static void main(String[] args) throws IOException {
		// 在工作簿里创建一个sheet,在sheet里创建一行,参数为行号(第二行)
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("new sheet");
		HSSFRow row = sheet.createRow((short) 0);

		// ------------创建单元格----------------
		// 不使用式样的日期
		row.createCell((short) 0).setCellValue(new java.util.Date());
		// 使用式样的日期
		HSSFCell cell = row.createCell((short) 1);
		cell.setCellValue(new java.util.Date());
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
		cell.setCellStyle(cellStyle);
		// 使用式样的数值
		cell = row.createCell((short) 2);
		cell.setCellValue(11111.25);
		cellStyle = wb.createCellStyle();
		cellStyle.setDataFormat(wb.createDataFormat().getFormat("#,##0.0000"));
		cell.setCellStyle(cellStyle);
		// 调用自定义方法createCell,使用对齐式样
		row = sheet.createRow((short) 1); // 换到第二行
		createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_LEFT);
		createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_FILL);
		createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_CENTER);
		createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_CENTER_SELECTION);
		createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_RIGHT);
		createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_GENERAL);
		createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_JUSTIFY);

		// 写入文件
		FileOutputStream fileOut = new FileOutputStream("c:\\workbook.xls");
		wb.write(fileOut);
		fileOut.close();
	}

	// 自定义方法,创建一个使用了对齐式样的单元格,column为列号
	private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align) {
		HSSFCell cell = row.createCell(column);
		cell.setCellValue("Giles");
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(align);
		cell.setCellStyle(cellStyle);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -