我编写了一个代码,该代码读取其中包含公式的excel工作表,并写入excel以输出excel。
在我的Excel中,我有类似日期|的列。天一个月年
日期列包含类似于1-12-2019的值,而在其他列(日,月,年)中,我有返回公式(TEXT(A5,“ ddd”),TEXT(A5,“ mmm”),TEXT(A5 ,yyyy))。
代码没有任何错误,但是当我在A5行上时。日,月,年列采用A6列的数据(TEXT(A6,“ ddd”等。)。
package com.opentext.shifttimes;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ShiftRecord {
ResourceBundle rs=ResourceBundle.getBundle("com.opentext.shifttimes.shift_parameters");
static Logger logger = Logger.getLogger(ShiftRecord.class);
String SHIFT_XLSX_FILE_PATH;
String FileName;
public void ReadExcelFiles()
{
try{
XSSFWorkbook outputWorkbook=new XSSFWorkbook();
if(!rs.getString("excel_files_path").equals("") && rs.getString("excel_files_path")!=null)
{
SHIFT_XLSX_FILE_PATH = rs.getString("excel_files_path");
}
else
{
SHIFT_XLSX_FILE_PATH="C:/Team";
}
if(!rs.getString("file_name").equals("") && rs.getString("file_name")!=null)
{
FileName = rs.getString("file_name");
//SHIFT_XLSX_FILE_PATH=SHIFT_XLSX_FILE_PATH+"/"+FileName;
logger.info("::::::: Printing Excel Path ::::::::: "+SHIFT_XLSX_FILE_PATH);
Workbook workbook = WorkbookFactory.create(new File(SHIFT_XLSX_FILE_PATH));
FileOutputStream fos=new FileOutputStream(rs.getString("excel_output_file_path"));
logger.info("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
logger.info("Retrieving Sheets using for-each loop");
for(Sheet sheet: workbook) {
logger.info(" Sheet Name : " + sheet.getSheetName());
}
Sheet sheet = workbook.getSheetAt(0);
getSheets(outputWorkbook,sheet);
// outputWorkbook.write(fos);
Sheet sheet1 = workbook.getSheetAt(1);
getSheets(outputWorkbook,sheet1);
outputWorkbook.write(fos);
// Step #10 : At the end of the Program close the FileOutputStream object.
fos.close();
}
else
{
FileName="";
}
}
catch(Exception e)
{
logger.info(" >>>>>> Got Exception in ReadExcelFiles >>>>>> "+e.getMessage());
e.printStackTrace();
}
}
public void getSheets(XSSFWorkbook outputWorkbook,Sheet sheet)
{
try
{
XSSFSheet outputSheet=outputWorkbook.createSheet(sheet.getSheetName());
// DataFormatter dataFormatter = new DataFormatter();
logger.info("\n\nIterating over Rows and Columns using Iterator\n");
// Shift of one row all the rows starting from the 3th row
sheet.shiftRows(2, sheet.getLastRowNum(), 1, true, true);
// Create my new 3th row
sheet.createRow(2);
int i=0;
for (Row row: sheet) {
int j=0;
//List<Object> data=new ArrayList<Object>();
logger.info("================== printing Rows =========================================="+row.getCell(7));
for(Cell cell: row) {
logger.info("================= Row No======="+i+"========");
//data.add(cell);
/*if( row.getCell(6)==null || row.getCell(6).equals("") || row.getCell(6).equals(null) || row.getCell(6).equals("-") || row.getCell(6).equals("Shift"))*/
/* if( row.getCell(7)==null || row.getCell(7).getStringCellValue().equals("") || row.getCell(7).equals(null))
{
logger.info("================= Shift is null not copying data for tis row===============");
}
else
{*/
printCellValue(outputSheet,i,j,cell);
/*}*/
j++;
}
i++;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
private static void printCellValue(XSSFSheet outputSheet,int i,int j,Cell cell) {
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
logger.info(" BOOLEAN : "+cell.getBooleanCellValue());
if(j==0)
{
outputSheet.createRow(i).createCell(j).setCellValue(cell.getBooleanCellValue());
}
else
{
outputSheet.getRow(i).createCell(j).setCellValue(cell.getBooleanCellValue());
}
break;
case STRING:
logger.info(" STRING : "+cell.getRichStringCellValue().getString());
if(j==0)
{
outputSheet.createRow(i).createCell(j).setCellValue(cell.getRichStringCellValue().getString());
}
else
{
outputSheet.getRow(i).createCell(j).setCellValue(cell.getRichStringCellValue().getString());
}
break;
case NUMERIC:
if(j==0)
{
if (DateUtil.isCellDateFormatted(cell)) {
logger.info(" DATE : "+cell.getDateCellValue());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String value = sdf.format(cell.getDateCellValue());
outputSheet.createRow(i).createCell(j).setCellValue(value);
} else {
logger.info(" NUMBER : "+cell.getNumericCellValue());
outputSheet.createRow(i).createCell(j).setCellValue(cell.getNumericCellValue());
}
}
else
{
if (DateUtil.isCellDateFormatted(cell)) {
logger.info(" DATE else : "+cell.getDateCellValue());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String value = sdf.format(cell.getDateCellValue());
outputSheet.getRow(i).createCell(j).setCellValue(value);
} else {
logger.info(" NUMBER else : "+cell.getNumericCellValue());
outputSheet.getRow(i).createCell(j).setCellValue(cell.getNumericCellValue());
}
}
break;
case FORMULA:
System.out.print(cell.getCellFormula());
if(j==0)
{
outputSheet.createRow(i).createCell(j).setCellFormula(cell.getCellFormula());
}
else
{
outputSheet.getRow(i).createCell(j).setCellFormula(cell.getCellFormula());
}
break;
case BLANK:
logger.info(" BLANK CELL ");
break;
default:
logger.info("");
}
System.out.println("\n");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
ShiftRecord records=new ShiftRecord();
records.ReadExcelFiles();
}
catch (Exception e) {
// TODO: handle exception
logger.info(" >>>> Got Exception in main>>>> "+e.getMessage());
e.printStackTrace();
}
}
}
我希望日,月和年列应获取相同的行数据,而不是下一行数据。
现在,如果日期为A5行中的1-12-2018,则日,月,年,列如下所示
day-sun(不正确。显示下一行,应该坐在这里)。 月-十二月 2018年。
请在上述问题上提供帮助。