public static TempFile createExcelFile(String sheetTitle, String title, String headerSummary, List<String> header, List<LinkedHashMap<String, String>> data)
{
TempFile result = new TempFile(FileKeyFactory.getFileKey(ExcelReporter.class).getFullPathKey());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet spreadSheet;
DataFormat format = null;
int rowIndex = 0;
// Get a DataFormat object and use it to create a CellStyle object
// with the following format set for the cells @. The @ or ampersand
// sets the format so that the cell will hold text.
format = wb.createDataFormat();
if (sheetTitle != null)
{
spreadSheet = wb.createSheet(sheetTitle);
}
else
{
spreadSheet = wb.createSheet();
}
if (title != null)
{
// Title
HSSFFont titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 14);
titleFont.setItalic(true);
HSSFRow titleRow = spreadSheet.createRow(rowIndex++);
HSSFCell cell = titleRow.createCell(0);
cell.setCellValue(title);
cell.getCellStyle().setFont(titleFont);
spreadSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 100));
}
else
{
spreadSheet = wb.createSheet();
}
HSSFCellStyle style = wb.createCellStyle();
style.setDataFormat(format.getFormat("@"));
if (headerSummary != null)
{
HSSFFont headerFont = wb.createFont();
headerFont.setFontHeightInPoints((short) 9);
headerFont.setItalic(false);
HSSFCellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setDataFormat(format.getFormat("@"));
headerCellStyle.setFont(headerFont);
headerCellStyle.setWrapText(true);
HSSFRow headerRow = spreadSheet.createRow(rowIndex++);
HSSFCell headerCell = headerRow.createCell(0);
headerCell.setCellValue(headerSummary);
headerCell.setCellStyle(headerCellStyle);
//increase row height to accommodate two lines of text
headerRow.setHeightInPoints((9 * spreadSheet.getDefaultRowHeightInPoints()));
//adjust column width to fit the content
spreadSheet.autoSizeColumn((short) 1);
spreadSheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 100));
}
/*
* Data table header
*/
HSSFRow dateRow = spreadSheet.createRow(rowIndex++);
Calendar calendar = Calendar.getInstance();
HSSFCreationHelper createHelper = wb.getCreationHelper();
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
HSSFCell cell = dateRow.createCell(0);
cell.setCellValue("Created: " + DateParser.toString(calendar, DateParser.MONTH_WORD_DATE_FORMAT));
cell.setCellStyle(cellStyle);
HSSFFont headerTableFont = wb.createFont();
headerTableFont.setFontHeightInPoints((short) 10);
headerTableFont.setColor(IndexedColors.WHITE.getIndex());
HSSFCellStyle tableHeaderStyle = wb.createCellStyle();
tableHeaderStyle.setDataFormat(format.getFormat("@"));
tableHeaderStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
tableHeaderStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
tableHeaderStyle.setFont(headerTableFont);
tableHeaderStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
HSSFRow tableHeaderRowRow = spreadSheet.createRow(rowIndex++);
for (int i = 0; i < header.size(); i++)
{
HSSFCell ipHeaderCell = tableHeaderRowRow.createCell(i);
ipHeaderCell.setCellStyle(tableHeaderStyle);
ipHeaderCell.setCellValue(header.get(i));
spreadSheet.autoSizeColumn(i);
}
/*
* ********************** DATA *******************
*/
HSSFCellStyle dataCellStyle = wb.createCellStyle();
dataCellStyle.setDataFormat(format.getFormat("@"));
dataCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
dataCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//spreadSheet.createRow(rowIndex++);
for (LinkedHashMap<String, String> row : data)
{
HSSFRow currentRow = spreadSheet.createRow(rowIndex++);
for (int i = 0; i < header.size(); i++)
{
HSSFCell dataCell = currentRow.createCell(i);
dataCell.setCellStyle(dataCellStyle);
dataCell.setCellType(Cell.CELL_TYPE_STRING);
String celVal = row.get(header.get(i));
dataCell.setCellValue(celVal);
}
}
// Resize columns automatically.
for (int i = 0; i < header.size(); i++)
{
spreadSheet.setDefaultColumnStyle(i, style);
spreadSheet.autoSizeColumn(i);
}
FileOutputStream resultOutStream = null;
try
{
resultOutStream = new FileOutputStream(result);
wb.write(resultOutStream);
resultOutStream.flush();
}
catch (Exception e)
{
Logger.ERROR("File path: " + result.getAbsolutePath() + File.separator + result.getName(), e);
throw new RuntimeException("File path: " + result.getAbsolutePath() + File.separator + result.getName(), e);
}
finally
{
try
{
if (resultOutStream != null)
{
resultOutStream.close();
}
}
catch (Exception ignore)
{
}
}
return result;
}
public void import()
{
...
InputStream input = new BufferedInputStream(new FileInputStream(file));
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
rows.next(); // skip title
rows.next(); // skip empty row
List<String> header = getLine((HSSFRow) rows.next());
...
}
private List<String> getLine(HSSFRow row)
{
List<String> header = new ArrayList<String>();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
HSSFCell cell = (HSSFCell)cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
header.add(cell.toString());
}
return header;
}
知道为什么在导出并再次导入后,调用sheet.rowiterator()时会忽略空行吗?
这是预期的行为。在Javadocs中:
返回物理行的迭代器。这意味着第3个元素可能不是第三行,比如说,第二行没有定义。
如果您查看关于在行和单元格上进行迭代的Apache POI文档,您将看到更详细的描述,以及如何强制在一个范围内的所有行上进行迭代的说明,如果这是您真正想要的。您的代码应该类似于(摘自Apache POI文档)
// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
if (r == null) {
// Handle empty row
continue;
}
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
}
}
}
我正在导入一个相当大的数据库。文件中几乎有1,000,000行。问题是我在尝试导入数据库时遇到语法错误。上面写着: 第8428420行错误1064(42000):您的SQL语法中有错误;检查与您的MySQL server版本相对应的手册,以获得在“ 致命错误:中超过600秒的最大执行时间”附近使用的正确语法 通常我会打开.sql文件并修复错误。但我的电脑真的很难打开这个文件。 在导入MySQL数据
本文向大家介绍java实现Excel的导入、导出,包括了java实现Excel的导入、导出的使用技巧和注意事项,需要的朋友参考一下 一、Excel的导入 导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性。由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正) 方式一、JXL导入 所需jar包 JXL.jar 方式二、POI
问题内容: 我已经找到了将HTML表导出到Excel的方法,但是在将JavaScript表导出从网站导出到excel时遇到了问题。请帮助我找到一种方法,将此表导出到Excel。 页面,从那里我需要信息: www.jalgpall.ee/players.php?year=2012&team=84&player=28469&b=ML 此页面javascript表函数的示例: 问题答案: 我自己找到了答
有没有办法从HTML表导出到excel。我在.NET核心应用程序中使用了HTML表格,需要用样式对表格进行格式化,并且需要导出到Excel。
本文向大家介绍Asp.Net使用Npoi导入导出Excel的方法,包括了Asp.Net使用Npoi导入导出Excel的方法的使用技巧和注意事项,需要的朋友参考一下 asp.net针对Excel文件的导入与导出是非常常见的功能之一。本文实例讲述了Asp.Net使用Npoi导入导出Excel的方法。分享给大家供大家参考之用。具体方法如下: 在使用Npoi导出Excel的时候,服务器可以不装任何offi