Here is my code:
try {
String filepath = Paths.get(“test.csv”).toAbsolutePath().toString();
final LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);
final Workbook csvFile = new Workbook(filepath, loadOptions);
final Worksheet worksheet = csvFile.getWorksheets().get(0);
worksheet.autoFitColumns();
final PageSetup pagesetup = worksheet.getPageSetup();
pagesetup.setFitToPagesTall(0);
pagesetup.setPaperSize(PaperSizeType.PAPER_A_4);
pagesetup.setPrintQuality(1200);
pagesetup.setPrintGridlines(true);
pagesetup.setPrintHeadings(true);
pagesetup.setPrintComments(PrintCommentsType.PRINT_IN_PLACE);
pagesetup.setPrintDraft(true);
pagesetup.setPrintErrors(PrintErrorsType.PRINT_ERRORS_NA);
pagesetup.setOrder(PrintOrderType.OVER_THEN_DOWN);
final String pdfFilePath = Paths.get(filepath.replace(".csv", "").concat(".pdf")).toAbsolutePath().toString();
PdfSaveOptions pso = new PdfSaveOptions();
pso.setGridlineType(GridlineType.HAIR);
csvFile.save(pdfFilePath, pso);
}
catch(Exception e){
e.printStackTrace();
}
Would you like to help me please with these issues?
Thanks.
Thanks for the template file and sample code segment.
Well, your mentioned issues are not concerned with Aspose.Cells APIs rather these are due to MS Excel (behavior) itself or due to your code. So, you need to update your code segment to cope with these issues/limitations. To confirm these issues, you may simply open your template file into MS Excel and specify some PageSetup attributes (in accordance with your written codes), now take the print preview of your sheet and you will see the almost same behavior (display) as per the output PDF file generated by Aspose.Cells. I would reply and provide details to your mentioned issues.
This is due to your line of code:
pagesetup.setPrintHeadings(true);
Same text is cut when you take the print preview of the sheet in MS Excel manually.
The best way to cope with it is you should wrap text of B3 cell and call auto-fit row and columns operation.
I have written the following sample code which may suit your needs and which will cope with your issues a bit:
e.g Sample code:
try {
String filepath = "e:\\test2\\files\\test.csv";
final LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);
final Workbook csvFile = new Workbook(filepath, loadOptions);
final Worksheet worksheet = csvFile.getWorksheets().get(0);
final PageSetup pagesetup = worksheet.getPageSetup();
//get the column's width of the B2 cell
double colWidth = worksheet.getCells().getColumnWidth(1);
//Set the wrapping text on for B3 cell.
Style style = worksheet.getCells().get("B3").getStyle();
style.setTextWrapped(true);
worksheet.getCells().get("B3").setStyle(style);
worksheet.autoFitRow(2);
worksheet.autoFitColumns();
pagesetup.setPaperSize(PaperSizeType.PAPER_A_4);
pagesetup.setPrintQuality(1200);
pagesetup.setPrintGridlines(true);
pagesetup.setPrintComments(PrintCommentsType.PRINT_IN_PLACE);
pagesetup.setPrintDraft(true);
pagesetup.setPrintErrors(PrintErrorsType.PRINT_ERRORS_NA);
pagesetup.setOrder(PrintOrderType.OVER_THEN_DOWN);
final String pdfFilePath = "f:\\files\\outasdfasdfasdf161.pdf";
PdfSaveOptions pso = new PdfSaveOptions();
pso.setAllColumnsInOnePagePerSheet(true);
csvFile.save(pdfFilePath, pso);
}
catch(Exception e){
e.printStackTrace();
}
I am afraid, there may not be any generic solution. How could you accomplish the task in generic ways? I do not think if there is any automatic way to cope with it in MS Excel apart from manual way as I devised the code segment. Anyways, if you know any way to accomplish the task (in generic way) in MS Excel manually, let us know with details and we can look into it soon.
I am afraid, your other issue was a different case. In short, you got to devise your own code to accomplish the task manually, see our previous reply with sample code:
Also, I tried to find some generic way (other than manual way) in MS Excel to accomplish the task but I could not find any.
I am using Java and not .Net.
However I have tried this aspose-cells-19.11.7.jar file, the project was compiled successfully but the issue is still reproduced, so it didn’t solve the issue.
Did you try to convert the latest csv file that I attached? files.zip (29.5 KB)
Yes I tried to convert your file to PDF file format using Aspose.Cells for Java v19.11.7 and it works fine. I will share my output file for your reference. In the mean time, could you make sure you are using correct version. You may print the version number for confirmation.
These are the files (original csv file + converted pdf file created with Aspose.Cells for Java 19.11.7: files.zip (29.5 KB)
The corrupted cells are: A1, B1, B2
A1: “count” instead of “country”
B1: “contine” instead of “continent”
B2: “Americ” instead of “America”.
I again did try the following sample code with your template file using Aspose.Cells for Java v19.11.7, it works fine and the output PDF file is Ok (A1, B1 and B2 cells are fine). Please find attached the output PDF file format for your referenced:
e.g Sample code:
String filepath = "f:\\files\\test.csv";
final LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);
final Workbook csvFile = new Workbook(filepath, loadOptions);
final Worksheet worksheet = csvFile.getWorksheets().get(0);
final PageSetup pagesetup = worksheet.getPageSetup();
Style style = worksheet.getCells().getStyle();
style.setTextWrapped(true);
worksheet.getCells().setStyle(style);
worksheet.autoFitColumns();
worksheet.autoFitRows();
pagesetup.setPaperSize(PaperSizeType.PAPER_A_4);
pagesetup.setPrintQuality(1200);
pagesetup.setPrintGridlines(true);
pagesetup.setPrintComments(PrintCommentsType.PRINT_IN_PLACE);
pagesetup.setPrintDraft(true);
pagesetup.setPrintErrors(PrintErrorsType.PRINT_ERRORS_NA);
pagesetup.setOrder(PrintOrderType.OVER_THEN_DOWN);
//final String pdfFilePath = Paths.get(filepath.replace(".csv", "").concat(".pdf")).toAbsolutePath().toString();
PdfSaveOptions pso = new PdfSaveOptions();
pso.setAllColumnsInOnePagePerSheet(true);
csvFile.save("f:\\files\\out1.pdf", pso);
By the way, what is your environment, especially display settings set for your os (I am using 100% display settings which is set (e.g in Control Panel\Appearance and Personalization\Display)). Please note, you should always set your display settings to 100% as Aspose.Cells works fine in 100% display settings. out1.pdf (28.2 KB)
Thanks for sharing your code because now I can see what is the issue.
Actually it is not the same code as mine but the relevant command that broke my result was:
Good to know that your issue is sorted out now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.