乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > Java 如何设置被导出excel单元格的<em>样式</em>?比如背景色,...

Java 如何设置被导出excel单元格的<em>样式</em>?比如背景色,...

作者:乔山办公网日期:

返回目录:excel表格制作


你好,我之前一直在研究poi和jxl,也遇到过类似的问题,你看是不是这样:
在excel 2003里,前面的单元格样式显示正常,但后面的单元格就没有样式了。但这个文件用excel 2007打开,就全都格式正常。

原因是,excel 2003只支持很有限的单元格样式,如果你同一种单元格样式创建了很多次,它会当作不同的单元格样式,存储在文件里,所以我最后的解决方法是:
<连在一起的表示属性的字符串,对应的单元格样式>存储在hashMap里,这样的话,可以有效的解决这个问题,我这个hashMap说的不知道你明白了没有。

使用 poi ,具体实现
HSSFCellStyle style = null;
// 创建表头style
HSSFCellStyle cellStyleTitle = workbook.createCellStyle();
cellStyleTitle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充单元格
cellStyleTitle.setFillForegroundColor(HSSFColor.YELLOW.index);
cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// //居中显示

HSSFRow titleRow = sheet.createRow(0);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = titleRow.createCell(i);
// cell.setCellStyle(createCellColorStyle(workbook));
cell.setCellStyle(cellStyleTitle);
cell.setCellValue(titles[i]);// 给单元格赋值
}

不知e799bee5baa6e59b9ee7ad94363道能,看懂不,如果有不清楚的私聊
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;

public class PoiCreateExcelTest {
    public static void main(String[] args) {
        /** 
         * @see <a href="http://poi.apache.org/hssf/quick-guide.html#NewWorkbook">For more</a>
         */        
        // 创建新的Excel 工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        
        // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
        HSSFSheet sheet = workbook.createSheet();
        //HSSFSheet sheet = workbook.createSheet("SheetName"); 
        
        // 用于格式化单元格的数据
        HSSFDataFormat format = workbook.createDataFormat();
        
        // 创建新行(row),并将单元格(cell)放入其中. 行号从0开始计算.
        HSSFRow row = sheet.createRow((short) 1);

        // 设置e68a84e8a2ade799bee5baa6e997aee7ad94336字体
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 20); //字体高度
        font.setColor(HSSFFont.COLOR_RED); //字体颜色
        font.setFontName("黑体"); //字体
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
        font.setItalic(true); //是否使用斜体
//        font.setStrikeout(true); //是否使用划线

        // 设置单元格类型
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
        cellStyle.setWrapText(true);
        
        // 添加单元格注释
        // 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器.
        HSSFPatriarch patr = sheet.createDrawingPatriarch();
        // 定义注释的大小和位置,详见文档
        HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
        // 设置注释内容
        comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
        // 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
        comment.setAuthor("Xuys.");
        
        // 创建单元格
        HSSFCell cell = row.createCell((short) 1);
        HSSFRichTextString hssfString = new HSSFRichTextString("Hello World!");
        cell.setCellValue(hssfString);//设置单元格内容
        cell.setCellStyle(cellStyle);//设置单元格样式
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);//指定单元格格式:数值、公式或字符串
        cell.setCellComment(comment);//添加注释

        //格式化数据
        row = sheet.createRow((short) 2);
        cell = row.createCell((short) 2);
        cell.setCellValue(11111.25);
        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(format.getFormat("0.0"));
        cell.setCellStyle(cellStyle);

        row = sheet.createRow((short) 3);
        cell = row.createCell((short) 3);
        cell.setCellValue(9736279.073);
        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
        cell.setCellStyle(cellStyle);
        
        
        sheet.autoSizeColumn((short)0); //调整第一列宽度
        sheet.autoSizeColumn((short)1); //调整第二列宽度
        sheet.autoSizeColumn((short)2); //调整第三列宽度
        sheet.autoSizeColumn((short)3); //调整第四列宽度

        try {
            FileOutputStream fileOut = new FileOutputStream("C:/3.xls");
            workbook.write(fileOut);
            fileOut.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

}


完整例子供参考:e799bee5baa6e59b9ee7ad94333
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
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.xssf.usermodel.XSSFWorkbook;

public class TestClass {
public static void main(String[] args){
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("coverageData");
int rownum = 0,cellnum=0;
Row row1 = sheet.createRow((short)rownum++);
//Set Color style start
CellStyle row1style = wb.createCellStyle();
row1style.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.getIndex());
row1style.setFillPattern(CellStyle.BIG_SPOTS);
//Set Color style end
Cell cell10 = row1.createCell((short)cellnum++);
cell10.setCellStyle(row1style);
cell10.setCellValue("cell data");
try{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("Spreadsheet.xlsx"));
wb.write(out);
out.close();
System.out.println("Spreadsheet.xlsx written successfully on disk.");
} catch (Exception e) { e.printStackTrace(); }
}
}

相关阅读

  • java生成excel设置列宽,汉字问题

  • 乔山办公网excel表格制作
  • POI类库是JAVA平台下操作EXCEL的类库,功能很强大。在使用POI导出excel表格时经常会出现文件名变成乱码的情况,POI导出excel表时文件名变成乱码是怎么回事呢,如何解决?今天我们要说的
  • 怎样将MySQL数据导出到Excel表中

  • 乔山办公网excel表格制作
  • C#把查询出来的数据导出到Excel代码的代码如下:1、创建formpublic Form1(){ InitializeComponent();}2、点击button开始查询db并导入excel数据private void button1_Click(object sender, Event
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网