Excel Buffer overwrites format NAV 2016
jwilder
Member Posts: 263
In the past(NAV 2009 R2) I have used the Excel buffer to import a spreadsheet make a few changes and export it back out. This worked nice and preserved the formatting unless I specifically change the formatting such as bolding or underline.
I am finding in NAV 2016 I am losing the formatting. If I simply import a sheet and export it out with no changes it loses all the formatting.
Here is a quick sample of what I am talking about:
//Read File into Excel Buffer
FullFileName := 'C:\Users\myusername\Desktop\Book1.xlsx';
OriginalFileName := FullFileName;
FullFileName := FileMgt.UploadFileSilent(FullFileName);
ExcelSheetName := ExcelBuffer.SelectSheetsName(FullFileName);
ExcelBuffer.OpenBook(FullFileName,ExcelSheetName);
ExcelBuffer.ReadSheet;
//Write File back to spreadsheet
ExcelBuffer.UpdateBook(FullFileName,ExcelSheetName);
ExcelBuffer.WriteSheet('Report Header','SWK',USERID);
ExcelBuffer.CloseBook;
ExcelBuffer.OverwriteAndOpenExistingExcel(OriginalFileName);
ExcelBuffer.GiveUserControl;
How do I preserve the formatting that already existed on the sheet such as cell color, word color etc...
I am finding in NAV 2016 I am losing the formatting. If I simply import a sheet and export it out with no changes it loses all the formatting.
Here is a quick sample of what I am talking about:
//Read File into Excel Buffer
FullFileName := 'C:\Users\myusername\Desktop\Book1.xlsx';
OriginalFileName := FullFileName;
FullFileName := FileMgt.UploadFileSilent(FullFileName);
ExcelSheetName := ExcelBuffer.SelectSheetsName(FullFileName);
ExcelBuffer.OpenBook(FullFileName,ExcelSheetName);
ExcelBuffer.ReadSheet;
//Write File back to spreadsheet
ExcelBuffer.UpdateBook(FullFileName,ExcelSheetName);
ExcelBuffer.WriteSheet('Report Header','SWK',USERID);
ExcelBuffer.CloseBook;
ExcelBuffer.OverwriteAndOpenExistingExcel(OriginalFileName);
ExcelBuffer.GiveUserControl;
How do I preserve the formatting that already existed on the sheet such as cell color, word color etc...
Jason Wilder
jwilder@stonewallkitchen.com
jwilder@stonewallkitchen.com
0
Best Answer
-
I do not know the former method that can accomplish this. Please tell me how to set the cell decorator so it does not overwrite.Jason Wilder
jwilder@stonewallkitchen.com5
Answers
-
please follow
http://saurav-nav.blogspot.co.uk/2015/08/nav-2013-r2-export-to-excel-with-font.html
http://saurav-nav.blogspot.co.uk/2016/03/nav-2013-r2-export-to-excel-with-font.htmlbest regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0 -
Thanks for responding Jonathan but unfortunately I am trying to preseve the font, style, color, format etc... on an already existing spreadsheet. I read the spreadsheet in (which does not read in anything about the format), process the data and then export it back out to the same spreadsheet, same cells.
When exporting out it forces some time of format and overwrites what is already there. I was looking for a way to set the cell decorator to "preserve original format" or "none" or something like that when exporting it back out.Jason Wilder
jwilder@stonewallkitchen.com1 -
please check if readsheet reads also the formatting. if not, thats a reason why.
also check writesheet. as i remember writesheet writes the values using default formatting settings. in the end some customization will be needed. using saurav's posting you will be able to do that.best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/1 -
Read sheet does not pull in the formatting. Yes writesheet use default settings but there is no setting for "none". So every time it writes to a cell it overwrites the formatting in excel base on the decorator logic in NAV. I need a null decorator or something like that.Jason Wilder
jwilder@stonewallkitchen.com0 -
in that case you'll need a customized excelbuffer for your need, kind of none-decorator or copy-decorator.best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0 -
I do not know the former method that can accomplish this. Please tell me how to set the cell decorator so it does not overwrite.Jason Wilder
jwilder@stonewallkitchen.com5 -
Agree but the dotnet control does not seem to have a method for this. Can you find one?Jason Wilder
jwilder@stonewallkitchen.com0 -
i will check that and write a blog post about it.best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0 -
Jonathan have you done a blog on this yet? I do not think this is possible with the excel dotnet methods/properties we currently have (NAV 2016). I would love for you to prove me wrong.Jason Wilder
jwilder@stonewallkitchen.com0 -
i checked it. it's not possible to write data back to the excel doc. without formatting. there is no NoneFormatDecorator.
So i developed a .net assembly which covers the needed functionality: writing data to and reading data from an excel doc without any formatting. when writing cell values are simply overwritten.
you can download the lib from: https://1drv.ms/u/s!ApMe9MLXVSbOdlGstmPs1ZGLgNc
sample code:
LOCAL TestXlsLib()
// open excel workbook
XlsWorkbook := XlsWorkbook.Workbook('c:\temp\test.xlsx');
// select sheet 'Sheet2'
XlsWorksheet := XlsWorksheet.Worksheet(XlsWorkbook,'Sheet2');
// read one value
value := XlsWorksheet.GetValue(1, 1);
MESSAGE(value);
// Write some values ... without format
// only overwrite values, formatting stays
XlsWorksheet.SetValue(1, 1, 'First Name');
XlsWorksheet.SetValue(1, 2, 'Last Name');
XlsWorksheet.SetValue(1, 3, 'Full Name');
XlsWorksheet.SetValue(1, 4, 'Salary');
// read one value
value := XlsWorksheet.GetValue(1, 1);
MESSAGE(value);
//XlsWorkbook.Save(); // save the current loaded xls doc.
// save the doc as new file
XlsWorkbook.SaveAs('c:\temp\test2.xlsx');
// close excel doc, close excel
XlsWorkbook.Close();
// if an error occurs ...
if XlsWorkbook.Error <> '' then
MESSAGE(XlsWorkbook.Error);
// the variables
//XlsWorkbook DotNet XlsLib.Workbook.'XlsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38613a2311532c9a'
//XlsWorksheet DotNet XlsLib.Worksheet.'XlsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38613a2311532c9a'
//value Text
best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0
Categories
- All Categories
- 75 General
- 75 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K 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
- 610 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 253 Dynamics CRM
- 103 Dynamics GP
- 6 Dynamics SL
- 1.5K Other
- 991 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 28 Design Patterns (General & Best Practices)
- Architectural Patterns
- 9 Design Patterns
- 4 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1K General Chat
- 1.6K Website
- 77 Testing
- 1.2K Download section
- 23 How Tos section
- 249 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions
