package com.raz.api.engine.apiengine.util;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class xlGen {
public static void main(String[] args) throws Exception {
//Create blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet spreadsheet = workbook.createSheet(" Employee Info ");
//Create row object
XSSFRow row;
//Colors
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
//style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Font font = workbook.createFont();
font.setColor(IndexedColors.RED.getIndex());
style.setFont(font);
//This data needs to be written (Object[])
Map < String, Object[] > empinfo =
new TreeMap < String, Object[] >();
empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
empinfo.put( "2", new Object[] { "tp01", "Gopal", "Technical Manager" });
empinfo.put( "3", new Object[] { "tp02", "Manisha", "Proof Reader" });
empinfo.put( "4", new Object[] { "tp03", "Masthan", "Technical Writer" });
empinfo.put( "5", new Object[] { "tp04", "Satish", "Technical Writer" });
empinfo.put( "6", new Object[] { "tp05", "Krishna", "Technical Writer" });
//Iterate over data and write to sheet
Set < String > keyid = empinfo.keySet();
int rowid = 0;
for (String key : keyid) {
row = spreadsheet.createRow(rowid++);
Object [] objectArr = empinfo.get(key);
int cellid = 0;
for (Object obj : objectArr) {
Cell cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
}
}
//cell1.setCellStyle(style);
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"));
workbook.write(out);
out.close();
System.out.println("Writesheet.xlsx written successfully");
}
}
总是很好读:繁忙的开发人员HSSF和XSSF特性指南。
在你的情况下,特别是填充和颜色。
你问题的内容不太清楚。但从您的代码示例来看,我怀疑需要为标题单元格设置一个特殊的样式。
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XlGen {
public static void main(String[] args) throws Exception {
//Create blank workbook
Workbook workbook = new XSSFWorkbook();
//Create cell style for header row
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font font = workbook.createFont();
font.setColor(IndexedColors.YELLOW.getIndex());
style.setFont(font);
//Create a blank sheet
Sheet spreadsheet = workbook.createSheet(" Employee Info ");
//This data needs to be written (Object[])
Map < String, Object[] > empinfo =
new TreeMap < String, Object[] >();
empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
empinfo.put( "2", new Object[] { "tp01", "Gopal", "Technical Manager" });
empinfo.put( "3", new Object[] { "tp02", "Manisha", "Proof Reader" });
empinfo.put( "4", new Object[] { "tp03", "Masthan", "Technical Writer" });
empinfo.put( "5", new Object[] { "tp04", "Satish", "Technical Writer" });
empinfo.put( "6", new Object[] { "tp05", "Krishna", "Technical Writer" });
//Iterate over data and write to sheet
Set < String > keyid = empinfo.keySet();
int rowid = 0;
//Create row object
Row row;
//Create cell object
Cell cell;
for (String key : keyid) {
row = spreadsheet.createRow(rowid++);
Object [] objectArr = empinfo.get(key);
int cellid = 0;
for (Object obj : objectArr) {
cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
if (rowid == 1) { // it is header row
cell.setCellStyle(style); // set style for header cells
}
}
}
for (int c = 0; c < empinfo.get("1").length; c++) {
spreadsheet.autoSizeColumn(c);
}
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"));
workbook.write(out);
out.close();
System.out.println("Writesheet.xlsx written successfully");
}
}
问题内容: 我正在使用iText 5创建pdf,并希望添加页脚。我所做的一切都像第14章中的“ iText in action”一书中所述。 没有错误,但没有显示页脚。有人可以告诉我我在做什么错吗? 我的代码: 问题答案: 您报告的问题无法重现。我以您的示例为例,并使用此事件创建了TextFooter示例: 请注意,我只创建了一次和实例,从而提高了性能。我还介绍了页脚和页眉。您声称要添加页脚,但实
问题内容: 我在将新的简单XWPFTable添加到XWPFHeader(或Footer)时遇到了严重的麻烦。不幸的是,似乎只有一个人遇到相同的问题(https://bz.apache.org/bugzilla/show_bug.cgi?id=57366#c0)。 有谁有办法实现这一目标? 任何帮助将不胜感激! 亲切的问候… 〜丹尼尔 问题答案: 这可以使用公共XWPFTable insertNew
亲切的问候... ~丹尼尔
嗨,我想在第一行搜索一个字符串,如果找到了,我想移动那一列。
我有一个要求,需要将数据验证添加到整个列中,而不是特定的单元格中。我查阅了ApachePOI的文档,发现了下面的示例 但是上面的例子为特定的单元格创建了一个下拉数据验证。在这种情况下,行0,列0。列中的其余单元格没有验证。但是在实际的excel文件中,我们可以这样做,所以这应该是可能的。我尝试并搜索了很多,但无法找到解决方案。请帮助...
问题内容: 我已经开发了一个Java Swing应用程序。 如何设置特定JDayChooser日期的背景颜色? 谢谢 问题答案: 终于找到了解决方法:D