Excel-buffer and Autofit of column-width

jensthomsen
Member Posts: 173
Hi
With help from Saurav Dhyani's blog (http://saurav-nav.blogspot.dk/2015/07/nav-2013-r2-multiple-sheets-in-excel.html) i've created an excel-fil containing several sheets by use of the Excel-buffer. I will save the created files directly to a folder without opening it, and that all works fine. But my problem now is that I want to implement 'Autofit' of the columns. When just opening the excel file (by calling
ExcelBuf.OpenExcel();
ExcelBuf.GiveUserControl();
) it works fine, but if I just save the files the changes in autofit doesn't seem to be saved?? If the solution would be some kind op post-processing of the excel files, that would be ok to:-)
I'm using Navision 2015 (build 42951)
With help from Saurav Dhyani's blog (http://saurav-nav.blogspot.dk/2015/07/nav-2013-r2-multiple-sheets-in-excel.html) i've created an excel-fil containing several sheets by use of the Excel-buffer. I will save the created files directly to a folder without opening it, and that all works fine. But my problem now is that I want to implement 'Autofit' of the columns. When just opening the excel file (by calling
ExcelBuf.OpenExcel();
ExcelBuf.GiveUserControl();
) it works fine, but if I just save the files the changes in autofit doesn't seem to be saved?? If the solution would be some kind op post-processing of the excel files, that would be ok to:-)
I'm using Navision 2015 (build 42951)
0
Best Answer
-
Try the following changes to the EV_SaveExcel function...
PROCEDURE EV_SaveExcel@1160180002(FilePath@1160180001 : Text); VAR FileNameClient@1160180000 : Text; BEGIN CloseBook; IF OpenUsingDocumentService('') THEN EXIT; IF NOT PreOpenExcel THEN EXIT; FileNameClient := FileManagement.DownloadTempFile(FileNameServer); FileNameClient := FileManagement.MoveAndRenameClientFile(FileNameClient,GetFriendlyFilename,FilePath); XlWrkBk := XlHelper.CallOpen(XlApp,FileNameClient); PostOpenExcel; XlHelper.CallSaveAs(XlWrkBk,FileNameClient) CloseBook; END;
5
Answers
-
Have a look in table 370 Excel Buffer, you'll see this code in PostOpenExcel()...
// autofit all columns on all worksheets XlHelper.AutoFitColumnsInAllWorksheets(XlWrkBk);
...create your own function with the above code and then call it before saving the workbook.0 -
I've allready puzzled around with this function, but couldn't get it working...How should the dotnet variables 'XlWrkBk' and 'XlHelper' (and perhaps 'XlApp' ) be instantiated???0
-
Actually just try calling the PostOpenExcel() function before calling CloseBook(), all the necessary dotnet variables should be instantiated at that point0
-
Doesn't seem to be working...XLWrkBk isn't instantiated correct. In the standard function 'OpenExcel()' this line is called before 'PostOpenExcel()':
XlWrkBk := XlHelper.CallOpen(XlApp,FileNameClient);
But I don't want to open the Excel-book but just saving it.0 -
Hov about loading the Excel file in some Excel-DotNet InterOp...? Has anyone any experience with that?0
-
Can you post the code you use to create the Excel spreadsheet, don't need the stuff that populates the data on the spreadsheet just all the framework stuff e.g. creating the workbook/worksheet, saving/closing it etc...0
-
I'm pretty much just using the standard functionality on table 370. I have added this function:
PROCEDURE CreateBookOnly@1160180001(SheetName@1160180003 : Text[250];ReportHeader@1160180002 : Text[80];CompanyName@1160180001 : Text[30];UserID2@1160180000 : Text;BookCreated@1160180004 : Boolean);
BEGIN
ExcelBookCreated := BookCreated;
NewSheetName := SheetName;
IF NOT BookCreated THEN
CreateBook(SheetName);
CurrentRow := 0;
CurrentCol := 0;
WriteSheet(ReportHeader, CompanyName, UserID2);
END;
Where 'ExcelBookCreated' and 'NewSheetName' are global variables. In function 'Writesheet' I've added this code:
CRLF := 10;
RecNo := 1;
TotalRecNo := COUNT + InfoExcelBuf.COUNT;
RecNo := 0;
//Code added >>
IF ExcelBookCreated THEN
IF ActiveSheetName <> NewSheetName THEN
XlWrkShtWriter := XlWrkBkWriter.AddWorksheet(NewSheetName);
//Code added <<
XlWrkShtWriter.AddPageSetup(OrientationValues.Landscape);
For witting the excel-book I fill the excel-buffer for every sheet, call my new function 'CreateBookOnly' and delete the buffer (important!). When finished writing the excel-book I call a new custom-function on table 370:
PROCEDURE EV_SaveExcel@1160180002(FilePath@1160180001 : Text);
VAR
FileNameClient@1160180000 : Text;
BEGIN
CloseBook;
IF OpenUsingDocumentService('') THEN
EXIT;
IF NOT PreOpenExcel THEN
EXIT;
FileNameClient := FileManagement.DownloadTempFile(FileNameServer);
FileManagement.MoveAndRenameClientFile(FileNameClient,GetFriendlyFilename,FilePath);
END;0 -
Try the following changes to the EV_SaveExcel function...
PROCEDURE EV_SaveExcel@1160180002(FilePath@1160180001 : Text); VAR FileNameClient@1160180000 : Text; BEGIN CloseBook; IF OpenUsingDocumentService('') THEN EXIT; IF NOT PreOpenExcel THEN EXIT; FileNameClient := FileManagement.DownloadTempFile(FileNameServer); FileNameClient := FileManagement.MoveAndRenameClientFile(FileNameClient,GetFriendlyFilename,FilePath); XlWrkBk := XlHelper.CallOpen(XlApp,FileNameClient); PostOpenExcel; XlHelper.CallSaveAs(XlWrkBk,FileNameClient) CloseBook; END;
5 -
Hi Kishorm
It seems to be working. To prevent being prompted for overwriting the file, I'm working on TEMP-files and uses only XlHelper.CallSaveAs to finalize the file. My code ended up like this:
CloseBook;
IF OpenUsingDocumentService('') THEN
EXIT;
IF NOT PreOpenExcel THEN
EXIT;
FileNameClient := FileManagement.DownloadTempFile(FileNameServer);
XlWrkBk := XlHelper.CallOpen(XlApp,FileNameClient);
PostOpenExcel;
FileNameClient_OpenedAndAutofitted := FileManagement.ClientTempFileName('xlsx');
FileManagement.CopyClientFile(FileNameClient,FileNameClient_OpenedAndAutofitted,TRUE);
FileNameClient_OpenedAndAutofitted := FileManagement.MoveAndRenameClientFile(FileNameClient_OpenedAndAutofitted,GetFriendlyFilename,FilePath);
FileManagement.DeleteClientFile(FileNameClient_OpenedAndAutofitted);
XlHelper.CallSaveAs(XlWrkBk,FileNameClient_OpenedAndAutofitted);
QuitExcel;
I,ve experienced problems with 'Excel' "holding" on to the file, but I think/hope I've solved it by calling the function 'QuitExcel' at the end:-)
MANY THX for helping me out!
0 -
Great @jensthomsen, In that case please select "Yes" on the "Did this answer your question? yes / no" option on my previous post so that others can quickly find the answer0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions