Excel Custom Data Format - excel

I'm using NPOI to generate XLS spreadsheets. NPOI is an Excel Spreadsheet generation library/API that is available on codeplex, enables you to create workbooks, formatting, formulae and so on and so forth....I use it to create workbooks with multiple sheets that contain the output of the various calculations.
I've used the following custom data format for each cell that contains a value, which I've plumbed into NPOI using code along the lines of:
var newFormat = MyNPOIWorkBook.CreateDataFormat();
var customFormat = newFormat.GetFormat("[=0]0;0.####");
the customFormat is then applied to a Cell within a method that creates the Cell Styles.
This works rather well - but not so well when the 4th decimal place after the point/period is a 0. When this is the case, I do not get the 0. My requirement is to have the 0 actually show!
So, to illustrate. 0.33445566 displays in the spreadsheet as 0.3343 (fine!) - but 0.3340 displays as 0.334 (not fine) - I want 0.3340. I appreciate that this is somewhat trivial, but I would like to satisfy my objective precisely :)
Can anybody help - either by suggesting a workaround, or altering my custom format in some magical fashion?
Thanks
-SB

How about [=0]0;0.###0?

Related

Spire When Pdf-ing an excel file it leaves some formulas as System.Object[][]

I have an excel template that has preset formulas, then have my wpf application fills in data in other sheets then the preset formulas takes the data from the other sheet and shows it on the main page.
The problem is when I automatically PDF the excel most formulas go through but other's give me a System.Object[][] or some other errors. But when I access the excel file with the dataset it works.
The difference between the formulas that go through and the ones that don't are the ones that have an if() statement to remove all non zeroes in a range like this.
=TEXTJOIN("
", TRUE, TEXT(IF(Details!O:O>0,Details!O:O,""), "HH:MM"))
Functions like this works:
=TEXTJOIN("
",TRUE,Details!D:D)
How do I get Spire.xls to PDF the right format?
As you tested the Excel file and found it working fine, the problem is likely located inside the Spire rendering.
You can of course report a bug over there but that might not get resolved instantly.
You may want to try applying a number format to the cells for zero values instead of using that if part in the formula.
Something like this could do when writing with ClosedXML if implemented correctly by Spire:
worksheet.Cell(row, column).Style.NumberFormat.Format = "#,##0;[Red]-#,##0;\"\";#";
Formating possibilities are somewhat documented by Microsoft, take a look over there.

VBA - Pretty Print a Row in Excel

I want to print a row in Excel.
One row contains data for one project.
I want to print one row on one page nicely formatted. Meaning the value of one cell is the header and should be printed fat and centred, the other values should also be placed at fixed positions on the page.
Is this with VBA possible? When I was searching for this problem I only found results for printing a worksheet or a table or parts of it, but no results to use the values of the cells and formatting them.
Thanks in advance
As the other answers indicate, it is certainly possible in Excel VBA, but it is not really Excel's strong point.
What would typically be done to obtain the result you seem to be after is use a fully formatted Word document with fields that are then filled in with values from an Excel worksheet. You can even cheat a bit and use the Mail Merge \ Letter wizard to set everything up.
If you do want to do it all in Excel, you can find instructions and an example VBA macro here:
http://www.tek-tips.com/faqs.cfm?fid=4223
Template is a good way to do. With a macro there's better performance where it avoids the usage of volatile functions such as INDIRECT() However again it depends on how many volatile functions your worksheet carries.
Yes, it is possible when you use the Styles in excel. I know you can do Font formatting quite easily. Not sure about indenting it, but worth a try.
If style doesnt support it (it might in Excel 2010), you can always indent it via VBA (record a macro when you indent the values , it should look like this):
Selection.InsertIndent 1

Is there a way in SpreadsheetML to force Excel to recalculate formulas (and other dependencies) when document is opened?

As far as I understand cells with formulas in Microsoft Office Excel can contain calculated values when serialized and saved in Office Open XML formats (specifically SpreadsheetML). This most likely applies to other types of dependencies and functions of values from other cells (like charts, pivot tables, etc.). I most likely do something wrong, but when processing this XML documents (SpreadsheetML) by external tools, that do not use any .Net components or similar APIs provided by MS, but just directly manipulating XML, I get into a problem that when I modify some content of one of worksheets Excel will still use last generated values in cells containing formulas. So when user opens generated spreadsheet he sees modified data but all the calculated fields are outdated. Now the only thing that I could find (these days) on internet was this:
http://openxmldeveloper.org/discussions/formats/f/14/p/1561/4164.aspx
This is really not a preferable solution especially if it applies to any kind of calculated cells and objects (charts, etc.) as it means partially reimplementing some SpreadsheetML processor when you do not know exactly the structure of all worksheets.
I would hope there would certainly be either an option in Excel or a configuration in one of the SpreadsheetML parts to force recalculations or to mark cells dirty, but I couldn't find one yet.
There is an assumption that scripting would help, but my lack of knowledge of that area didn't brought me to any successful results yet as I'm not sure how to include scripts into SpreadsheetML worksheet. Though I found quite some examples how to trigger recalculation and how to add open event listeners.
The easiest way is to remove the calculated value from the cell (as also noted in the link you provided).
You do not have to know the exact structure of worksheets. Just remove all occurrences of <v>#VALUE!</v> in worksheets/sheet1.xml (so that other functions will not be affected).
press F9 to recalculate all open workbooks
Excel Recalculation
Perhaps your Calculation Mode for the workbook is getting set to manual. Force this mode to Automatic when you open the workbook by setting it to null in the code with the following:
public void SetAutomaticCalculationMode(WorkbookPart workbookPart1)
{
Workbook workbook1 = workbookPart1.Workbook;
CalculationProperties calculationProperties1=workbook1.GetFirstChild<CalculationProperties>();
calculationProperties1.CalculationMode = null;
}
This will correspond to Automatic calculation mode as seen in the Options of Excel 2007 client:

Programmatically format a date in an excel sheet using Office Open Xml SDK

I'm building an Excel xlsx spreadsheet using the office open XML SDK. I can add dates to the sheet by converting them to their "AO" date representation and setting the Cell Value to number. I can't, however, figure out how to add the dd-mm-yy formatting to the cell.
I've seen a lot of complicated examples where you have to create a stylesheet from scratch, add the format to it and then reference that format, but I figure there must be an easier way. I'm creating my sheet from a template, and Excel already has builtin formats/styles. IMO I should be able to just load the stylesheet from the excel file I'm using as a template and then apply the format. Can't figure out how to do it though.
If you're creating an xlsx from scratch you can't avoid creating the style sheet. You minimally need a style sheet and cell format element. The cell format element can reference a built-in date format in which case you can avoid the custom number format, but if you want a date format that isn't built-in you need a number format element too.
If you're creating an xlsx from a template xlsx, then you can reference a cell format that's already defined, but it can be very sensitive since the references are based only on index, so you are safer looking through the defined cell formats to find the one that matches the date format you want as opposed to hard-coding the cell format index.
Try this - I used it when I was trying to make my own number formats. It's a good example:
Advanced Styling in Excel OpenXML
You will still have to create a style sheet but this is a very good place to start. He has quite a few styles in his as he demonstrates what can be done. Your style sheet could be lots simpler if you only want the one format.
Also have a quick look at the list in this post. Your format may be built into Excel already.
Built in Styles for Excel

Modify an Excel sheet from Matlab

Is it possible to open a worksheet in excel from matlab and edit the formulas? The idea is to automate an uncertainty analysis by creating a second sheet with the uncertainty in each cell for the value from the previous cell. Essentially, I want to treat the cells as variables and do SQRT(SUM(Partials(xi)^2)) for each cell. Matlab should have no problem with the calc, but can it edit the formulas in sheets?
The process currently is to copy and paste from excel to matlab. Here's a small function that does the uncertainty in matlab against on array of equations:
function [f_u_total f_u] = uncertAnalysis(f, vars, vars_u)
f_u = [];
f_u_total = [];
for(i=1:length(f))
f(i)
item = uncertAnalysisi(f(i), vars, vars_u);
f_u = [f_u; item(1)];
f_u_total = [f_u_total; item(1)];
end
end
function [f_u_total f_u] = uncertAnalysisi(f, vars, vars_u)
f_u = [];
% take the partials and square them
for i=1:length(vars)
f_u = [f_u; vars(i) (diff(f, vars(i)).*vars_u(i)).^2];
end
% calculate the RSS
f_u_total = (sum(f_u(:,2))).^.5;
end
As an aside, the equations look something like this (why I'm not doing this by hand):
=(9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^4/C!Y3^6/(C!U3^C!Z3)^6*F3^2+1/4*C!S3^2/C!V3^4*C!W3/(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*O3^2+1/4*C!S3^2/C!V3^4*C!W3/(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*P3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^4*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*Q3^2+1/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*S3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*C!Z3^2/C!U3^2*U3^2+4*C!S3^2/C!V3^6*C!W3*(C!O
3-C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*V3^2+1/4*C!S3^2/C!V3^4/C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*W3^2+1/4*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3^3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*X3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^8/(C!U3^C!Z3)^6*Y3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*LOG(C!U3)^2*Z3^2)^(1/2)
You should be able to do it through COM/ActiveX/Automation. Look at the External Interfaces document; there's an example for how to access Excel documents through Excel's Automation interfaces.
I have next-to-no experience manipulating Excel in this manner, but I know you can do just about anything in Excel through Automation and editing cell formulas doesn't sound that hard.
edit: I can't find a reference to the Excel object model, but here's another example: http://support.microsoft.com/kb/301982
This isn't a terribly elegant solution, but if you save a new .xls spreadsheet that's simply a tab-delimited (or CSV) file, you can have Matlab generate formulas and when Excel opens the document the values will populate.
In Perl, I've handled it something like this:
open(OUTPUT,'>tmpfile.xls');
print OUTPUT "1\t2\t=A1+B1\n";
close(OUTPUT);
And when tmpfile.xls is opened in Excel, cell C1 will display as 3, which will dynamically update appropriately if A1 or B1 are changed.
(I'm not good with Matlab, so I have no knowledge of any sort of plugins)
EDIT: My previous assumption that XLSWRITE wouldn't work was wrong. I just tried the following in MATLAB:
xlswrite('xltest.xls',{'1' '2' '=SUM(A1,B1)'});
and when I opened the file in excel, the function was in fact there! The limitation on this would be that you would have to use only the functions that are in Excel.
Unfortunately, I don't believe XLSREAD can read the formulae into MATLAB (it appears to just get the result).
PREVIOUSLY SUGGESTED OPTIONS:
You may want to check out the Spreadsheet Link EX software on the MathWorks website, although I'm a bit unfamiliar with it and am not sure if even that can do what you need. Something else that you should look into is MATLAB Builder EX, which "lets you integrate MATLAB® applications into your organization's Excel® workbooks as macro functions or add-ins". Sounds promising...
Use COM/ActiveX. You can open an Excel instance via the following command:
xlApp = COM.Excel.Application;
Then use a combination of code completion and the VBA help in Excel itself to work out the rest.
Remember to close Excel with
xlApp.Quit;
delete(xlApp);
On a side note, so-called CSE (Control-Shift-Enter) formulae may help? See Google.
As an alternative, see the code below (xlswrite) for using ActiveX from Matlab:
http://www.mathworks.com/matlabcentral/fileexchange/2855

Resources