POI做导出Excel设置单元格中字体大小颜色,合并行列
2013-06-06 17:04
786 查看
public class ExcelTest {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
/**字体begin*/
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//背景颜色
// style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成一个字体
HSSFFont font=wb.createFont();
font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色
font.setFontHeightInPoints((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗
//把字体应用到当前的样式
style.setFont(font);
/**字体end*/
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) 0, 5, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setCellValue("项目\\日期"); // 表格的第一行第一列显示的数据
ce.setCellStyle(style); // 样式,居中
int num = 0;
for (int i = 0; i < 9; i++) { // 循环9次,每一次都要跨单元格显示
// 计算从那个单元格跨到那一格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 单元格合并
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨单元格显示的数据
cell.setCellStyle(style); // 样式
// 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
num++;
}
// 在后面加上合计百分比
// 合计 在最后加上,还要跨一个单元格 //四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setCellValue("合计");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
// 百分比 同上
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);
//输出一些数据 然后再输出表头
FileOutputStream fileOut = new FileOutputStream("D://workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}
}
//设置单元格字体颜色
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class test {
public static void main(String[] args) {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
CreationHelper helper = workbook.getCreationHelper();
RichTextString str = helper.createRichTextString("a\nb\nc\nd\ne\n");// 在这里使用\n表示回车
Font[] fonts = new Font[5];
fonts[0] = workbook.createFont();
fonts[0].setColor(HSSFColor.YELLOW.index);
fonts[1] = workbook.createFont();
fonts[1].setColor(HSSFColor.RED.index);
fonts[2] = workbook.createFont();
fonts[2].setColor(HSSFColor.BLUE.index);
fonts[3] = workbook.createFont();
fonts[3].setColor(HSSFColor.ROSE.index);
fonts[4] = workbook.createFont();
fonts[4].setColor(HSSFColor.BLACK.index);
for (int i = 0; i < 5; i++) {
str.applyFont(i * 2, (i + 1) * 2, fonts[i]);
}
cell.setCellValue(str);
try {
FileOutputStream out = new FileOutputStream(new File("d:\\1.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
/**字体begin*/
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//背景颜色
// style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成一个字体
HSSFFont font=wb.createFont();
font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色
font.setFontHeightInPoints((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗
//把字体应用到当前的样式
style.setFont(font);
/**字体end*/
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) 0, 5, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setCellValue("项目\\日期"); // 表格的第一行第一列显示的数据
ce.setCellStyle(style); // 样式,居中
int num = 0;
for (int i = 0; i < 9; i++) { // 循环9次,每一次都要跨单元格显示
// 计算从那个单元格跨到那一格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 单元格合并
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨单元格显示的数据
cell.setCellStyle(style); // 样式
// 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
num++;
}
// 在后面加上合计百分比
// 合计 在最后加上,还要跨一个单元格 //四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setCellValue("合计");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
// 百分比 同上
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);
//输出一些数据 然后再输出表头
FileOutputStream fileOut = new FileOutputStream("D://workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}
}
//设置单元格字体颜色
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class test {
public static void main(String[] args) {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
CreationHelper helper = workbook.getCreationHelper();
RichTextString str = helper.createRichTextString("a\nb\nc\nd\ne\n");// 在这里使用\n表示回车
Font[] fonts = new Font[5];
fonts[0] = workbook.createFont();
fonts[0].setColor(HSSFColor.YELLOW.index);
fonts[1] = workbook.createFont();
fonts[1].setColor(HSSFColor.RED.index);
fonts[2] = workbook.createFont();
fonts[2].setColor(HSSFColor.BLUE.index);
fonts[3] = workbook.createFont();
fonts[3].setColor(HSSFColor.ROSE.index);
fonts[4] = workbook.createFont();
fonts[4].setColor(HSSFColor.BLACK.index);
for (int i = 0; i < 5; i++) {
str.applyFont(i * 2, (i + 1) * 2, fonts[i]);
}
cell.setCellValue(str);
try {
FileOutputStream out = new FileOutputStream(new File("d:\\1.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- POI做导出Excel设置单元格中字体大小颜色,合并行列
- 3、excel 导出。适合用于将界面上的内容所见及所得的输出,可以设置单元格的的合并,大小,字体,颜色等等
- POI导出Excel(设置字体,字体大小,行高,边框),让Excel更优美。(裴东辉)
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- PHPExcel-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- ApachePOI导出exce,设置单元格风格的属性和设置字体风格的属性的两个工具类,POI宽度和excel 像素转换
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- 黄聪:phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- PHPExcel-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- Java POI导出Excel时,字体单元格颜色示例
- Java poi导出excel合并标题单元格
- java poi设置Excel单元格颜色
- jxl导出excel(合并行、合并列、单元格居中、单元格固定宽度、字体加粗、分页签)
- java 使用poi导出Excel,设置单元格保护不可编辑,设置下拉框
- POI中设置Excel单元格格式样式(居中,字体,边框等)
- C#操作Excel(打开、保存方式,设置单元格、字体操作,c#颜色转换为Excel颜色的方式)
- 使用poi对excel条件格式设置字体颜色使用自定义的颜色
- Poi导出excel设置单元格数值格式