0

I am trying generate excel sheet using aspose. I have generated the excel sheet and I am trying add few data into it. I have added Response at cell A4. I want to add Date at cell B4 and Time at cell C4. I am not able to add B4 and C4 data.

Worksheet workSheet = workerBook.getWorksheets().get(0);
style.setTextWrapped(true);
workSheetCell = workSheet.getCells().get("A1");
workSheet.getCells().merge(3, 0, headerRows, 1);
workSheet.getCells().merge(3, 1, headerRows, 1);
workSheet.getCells().merge(3, 2, headerRows, 1);
// workSheet.getCells().get("C4").putValue("Time");
workSheet.getCells().get("A4").putValue("Respondent");

workSheet.getCells().get("B4").putValue("Date");

workSheet.getCells().get("C4").putValue("Time");

What is wrong here?

pars
  • 153
  • 2
  • 4
  • 11

2 Answers2

2

Cell.putValue() has many overloads, you can add the time and date values using one of these overloads.

Please try the following code, it should work fine. It adds a date in cell B4 and time in cell C4.

I have tested it with the latest version: Aspose.Cells for Java v7.3.2

In case, you still have questions, please post on Aspose.Cells forum.

Java

Workbook workerBook = new Workbook();

Worksheet worksheet = workerBook.getWorksheets().get(0);

Cell cellB4 = worksheet.getCells().get("B4");

//Add date value without time part
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
cellB4.putValue(c);

Style style = cellB4.getStyle();
style.setCustom("yyyy-mm-dd");
cellB4.setStyle(style);

Cell cellC4 = worksheet.getCells().get("C4");

//Add time value without date part
c = Calendar.getInstance();
c.set(1899, 11, 31);
cellC4.putValue(c);

style = cellC4.getStyle();
style.setCustom("hh:mm:ss");
cellC4.setStyle(style);

workerBook.save("output.xlsx", SaveFormat.XLSX);
shakeel
  • 1,717
  • 10
  • 14
  • i wanted to display the word "Date" and "Time" at columns B4 and C4,but its not getting displayed.Even your code is not working out. – pars Oct 09 '12 at 08:28
1

That should be even easier. Check the code below.

I have also now shown a screenshot showing the output of the code.

Output Screenshot

If this is not what you want, then please elaborate yourself more.

Java

Workbook workerBook = new Workbook();

Worksheet worksheet = workerBook.getWorksheets().get(0);

//Display word "Date" in cell B4
Cell cellB4 = worksheet.getCells().get("B4");
cellB4.putValue("Date");

//Display word "Time" in cell C4
Cell cellC4 = worksheet.getCells().get("C4");
cellC4.putValue("Time");

//Save the workbook in xlsx format
workerBook.save("output.xlsx", SaveFormat.XLSX);
shakeel
  • 1,717
  • 10
  • 14