Java 标题后的空行
我通过合并许多excel文件来编写现有的excel,生成最终的excel文件后,标题后面的空行相加 下面是我的代码,它从多个文件中读取数据,并写入设置了透视公式的特定空白文件 我甚至试过了 1.设置Java 标题后的空行,java,excel,apache-poi,Java,Excel,Apache Poi,我通过合并许多excel文件来编写现有的excel,生成最终的excel文件后,标题后面的空行相加 下面是我的代码,它从多个文件中读取数据,并写入设置了透视公式的特定空白文件 我甚至试过了 1.设置createRow(0),然后开始从下一行填充数据。 2.试图维护int计数器,但仍然不起作用 3.尝试递增getLastRowNum()count,但没有用 public class DCSReadImpl implements ReadBehavior { Logger log = Lo
createRow(0)
,然后开始从下一行填充数据。
2.试图维护int
计数器,但仍然不起作用
3.尝试递增getLastRowNum()
count,但没有用
public class DCSReadImpl implements ReadBehavior {
Logger log = Logger.getLogger(DCSReadImpl.class.getName());
@SuppressWarnings("resource")
@Override
public Sheet readReport(Workbook workbook,Map<String,String> masterMap, Properties properties) {
//int firstRow = 0;
int outRowCounter = 0;
String fileToMove= "";
boolean headers = true;
Row outputRow = null;
Sheet outputSheet = null;
Workbook wb = new XSSFWorkbook();
try {
outputSheet = wb.createSheet("Data");
log.info("**** Set headers start"); // this used to be different method
int cellNo = 0;
outputRow = outputSheet.createRow(0);
for(String headerName : ReportConstants.DCS_OUTPUT_HEADER){
outputRow.createCell(cellNo).setCellValue(headerName);
cellNo++;
}
//outRowCounter++;
log.info("**** Set headers completed");
log.info("Read input file(s) for DCS report");
log.info("Input File Path : " + properties.getProperty(ReportConstants.DCS_INPUT_PATH));
File inputDir = new File(properties.getProperty(ReportConstants.DCS_INPUT_PATH));
File[] dirListing = inputDir.listFiles();
if (0 == dirListing.length) {
throw new Exception(properties.getProperty(ReportConstants.DCS_INPUT_PATH) + " is empty");
}
for (File file : dirListing) {
log.info("Processing : " + file.getName());
fileToMove = file.getName();
XSSFWorkbook inputWorkbook = null;
try {
inputWorkbook = new XSSFWorkbook(new FileInputStream(file));
} catch (Exception e) {
throw new Exception("File is already open, please close the file");
}
XSSFSheet inputsheet = inputWorkbook.getSheet("Sheet1");
Iterator<Row> rowItr = inputsheet.iterator();
int headItr = 0;
//log.info("Validating headers : " + file.getName());
while (rowItr.hasNext()) {
Row irow = rowItr.next();
Iterator<Cell> cellItr = irow.cellIterator();
int cellIntItr = 0;
String key = "";
int rowN = outputSheet.getLastRowNum() + 1;
outputRow = outputSheet.createRow(rowN);
Cell outCell = null;
while (cellItr.hasNext()) {
Cell inputCell = cellItr.next();
if (0 == inputCell.getRowIndex()) {
if (!FileUtility.checkHeaders(headItr, inputCell.getStringCellValue().trim(),
ReportConstants.DCS_INPUT_HEADER)) {
throw new Exception("Incorrect header(s) present in Input File, Expected : "
+ ReportConstants.DCS_INPUT_HEADER[headItr]);
}
headItr++;
} else {
//outCell = outputRow.createCell(cellIntItr);
if (0 == inputCell.getColumnIndex()) {
key = inputCell.getStringCellValue().trim();
} else if (2 == inputCell.getColumnIndex()) {
key = key + ReportConstants.DEL + inputCell.getStringCellValue().trim();
}
if (7 == cellIntItr){
outCell = outputRow.createCell(cellIntItr);
outCell.setCellValue(getValue(masterMap, key, 0));
cellIntItr++;
outCell = outputRow.createCell(cellIntItr);
outCell.setCellValue(getValue(masterMap, key, 1));
cellIntItr++;
outCell = outputRow.createCell(cellIntItr);
outCell.setCellValue(getValue(masterMap, key, 2));
cellIntItr++;
}
// Check the cell type and format accordingly
switch (inputCell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
outCell = outputRow.createCell(cellIntItr);
outCell.setCellValue(inputCell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
outCell = outputRow.createCell(cellIntItr);
outCell.setCellValue(inputCell.getStringCellValue().trim());
break;
}
cellIntItr++;
}
}
//outRowCounter ++ ;
}
if(!fileToMove.isEmpty()){
FileUtility.checkDestinationDir(""+properties.get(ReportConstants.DCS_ARCHIVE_PATH));
FileUtility.moveFile(properties.get(ReportConstants.DCS_INPUT_PATH) + fileToMove,
properties.get(ReportConstants.DCS_ARCHIVE_PATH)+fileToMove+FileUtility.getPattern());
}
}
} catch (Exception e) {
log.error("Exception occured : ", e);
}
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream("D:\\DCS\\Output\\Krsna_"+FileUtility.getPattern()+".xlsx");
wb.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
}
return outputSheet;
}
private String getValue(Map<String, String> masterMap, String cellKey, int index) {
String value = masterMap.get(cellKey);
if (null != value) {
String cellValue[] = value.split("\\" + ReportConstants.DEL);
return cellValue[index];
} else {
return "";
}
}
}
公共类DCSReadImpl实现ReadBehavior{
Logger log=Logger.getLogger(DCSReadImpl.class.getName());
@抑制警告(“资源”)
@凌驾
公共图纸读取报告(工作簿、地图主地图、属性){
//int firstRow=0;
int outRowCounter=0;
字符串fileToMove=“”;
布尔头=真;
Row outputRow=null;
Sheet outputSheet=null;
工作簿wb=新XSSFWorkbook();
试一试{
outputSheet=wb.createSheet(“数据”);
log.info(“**Set headers start”);//这以前是不同的方法
int cellNo=0;
outputRow=outputSheet.createRow(0);
for(字符串头名称:ReportConstants.DCS\u输出头){
outputRow.createCell(cellNo).setCellValue(headerName);
cellNo++;
}
//outRowCounter++;
log.info(“****已完成设置标题”);
log.info(“读取DCS报告的输入文件”);
log.info(“输入文件路径:”+properties.getProperty(ReportConstants.DCS_Input_Path));
File inputDir=新文件(properties.getProperty(ReportConstants.DCS_INPUT_PATH));
File[]dirListing=inputDir.listFiles();
if(0==dirListing.length){
抛出新异常(properties.getProperty(ReportConstants.DCS_INPUT_PATH)+“为空”);
}
for(文件:dirListing){
log.info(“处理:“+file.getName());
fileToMove=file.getName();
XSSFWorkbook inputWorkbook=null;
试一试{
inputWorkbook=新XSSFWorkbook(新文件InputStream(文件));
}捕获(例外e){
抛出新异常(“文件已打开,请关闭文件”);
}
XSSFSheet inputsheet=inputWorkbook.getSheet(“Sheet1”);
迭代器rowItr=inputsheet.Iterator();
int headItr=0;
//log.info(“验证头:“+file.getName()”);
while(rowItr.hasNext()){
Row irow=rowItr.next();
迭代器cellItr=irow.cellIterator();
int-cellIntItr=0;
字符串键=”;
int rowN=outputSheet.getLastRowNum()+1;
outputRow=outputSheet.createRow(rowN);
Cell-outCell=null;
while(cellItr.hasNext()){
Cell inputCell=cellItr.next();
如果(0==inputCell.getRowIndex()){
如果(!FileUtility.checkHeaders)(headItr,inputCell.getStringCellValue().trim(),
ReportConstants.DCS_输入_标题){
抛出新异常(“输入文件中存在不正确的头,应为:”
+ReportConstants.DCS_输入_头[headItr]);
}
headItr++;
}否则{
//outCell=outputRow.createCell(cellIntItr);
如果(0==inputCell.getColumnIndex()){
key=inputCell.getStringCellValue().trim();
}else if(2==inputCell.getColumnIndex()){
key=key+ReportConstants.DEL+inputCell.getStringCellValue().trim();
}
if(7==cellIntItr){
outCell=outputRow.createCell(cellIntItr);
setCellValue(getValue(masterMap,key,0));
cellIntItr++;
outCell=outputRow.createCell(cellIntItr);
setCellValue(getValue(masterMap,key,1));
cellIntItr++;
outCell=outputRow.createCell(cellIntItr);
setCellValue(getValue(masterMap,key,2));
cellIntItr++;
}
//检查相应的单元格类型和格式
开关(inputCell.getCellType()){
case Cell.Cell\u类型\u数值:
outCell=outputRow.createCell(cellIntItr);
outCell.setCellValue(inputCell.getNumericCellValue());
打破
case Cell.Cell\u类型\u字符串:
outCell=outputRow.createCell(cellIntItr);
setCellValue(inputCell.getStringCellValue().trim());
打破
}
cellIntItr++;
}
}
//outRowCounter++;
}
如果(!fileToMove.isEmpty()){
FileUtility.checkDestinationDir(“+properties.get(ReportConstants.DCS\u ARCHIVE\u PATH));
FileUtility.moveFile(properties.get(ReportConstants.DCS\u INPUT\u PATH)+fileToMove,
properties.get(ReportConstants.DCS\u ARCHIVE\u PATH)+fileToMove+FileU