How to set displayed decimals upon creating Excel file? - c#

I am using the Interop library to create an Excel file and to write a double values to it, for example '1234.5678'. A lot of those values have a high number of decimals and displaying those decreases the readability of them. Also I do not want to loose those decimal places, so rounding is no option.
Excel has a nice way of dealing with this, since it lets you control the number of shown decimals using the buttons 'Increase Decimal' and 'Decrease Decimal' under the Home tab.
What I need however, is a way to set the number of shown decimal places directly from my application, is there a way to do this?
I use the following code to create a the file.
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel._Application xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = 1234.5678;

xlWorkSheet.Cells[1, 1].NumberFormat = "0.000";
"0.000" would display a number with 3 decimal points.
Full list of formats:
MSDN

Related

XValue propery in excel interop

I want to change the Xvalues in excel chart using Interop.
Following is what I am using. But it is not recognising Range.
Can anybody help me solve this?
chartPage.SetSourceData Range("A2:A4"), xlColumns
chartPage.SeriesCollection(1).XValues = Range("B2:B4")
It is not accepting Range, everywhere I am finding same answer.
Try to get the active worksheet and use range on it. Something like this:
_Worksheet ws = this.ActiveSheet
chartPage.SeriesCollection(1).XValues = ws.Range("B2:B4")

C# read multiple Excel files [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it possible to make an application that reads multiple excel files from a folder and extracts some information from them?
Yes it is, and here is how using Interop. The first thing you need to do is add the Excel Interop library to your project. You can do this by creating a new Visual Studio solution, right clicking on References, selecting Add Reference and then selecting Microsoft.Office.Interop.Excel from the .NET tab.
Then you need to add a using statement for Excel, and one for InteropServices (as we are interoping with a COM object):
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
Then, inside a method, you need to create an Application object:
Excel.Application application = new Excel.Application();
Next, create a Workbook object for each workbook you want to read from, like so:
Excel.Workbook workbookOne;
Excel.Workbook workbookTwo;
Excel.Workbook workbookThree;
Now use the Application object to open each workbook, and load each one into its respective Workbook object:
workbookOne = application.Workbooks.Open(#"C:\workbookOneLocation.xlsx");
workbookTwo = application.Workbooks.Open(#"C:\workbookTwoLocation.xlsx");
workbookThree = application.Workbooks.Open(#"C:\workbookThreeLocation.xlsx");
Now you need to decide what information you want to extract. Once you have done this determine which worksheet in the workbook it is on,and then figure out the number by simply looking at the tabs and counting. In the below example, Sheet2 is number 1, Sheet1 is number 2 and Sheet3 is number 3:
Create a variable for each piece of information you need like so (any value type variables will need to be nullable):
string firstPieceOfInformationINeed;
string[] secondPieceOfInformationINeed;
double? thirdPieceOfInformationINeed;
Let's say that the first piece of information we need is a string in Cell A1 on sheet one inside workbook one, the second piece is Cells B2 - B4 on worksheet two inside workbook two, and the third piece is a number on cell C5 on worksheet three inside workbook three. We would do this:
string firstPieceOfInformationINeed;
string[] secondPieceOfInformationINeed;
double? thirdPieceOfInformationINeed;
Excel.Worksheet worksheet;
Excel.Range range;
worksheet = workbookOne.Sheets[1];
range = worksheet.Cells["1", "1"];
firstPieceOfInformationINeed = range.Value as string;
worksheet = workbookTwo.Sheets[2];
range = worksheet.Range["B2", "B4"];
secondPieceOfInformationINeed = range.Value as string[];
worksheet = workbookThree.Sheets[3];
range = worksheet.Cells["3", "5"];
thirdPieceOfInformationINeed = range.Value as double?;
Then we close the workbook, using a boolean value to indicate whether or not we want to save changes:
workbookOne.Close(true);
workbookTwo.Close(false);
workbookThree.Close(true);
Now quit the application:
application.Quit();
And release the COM object:
Marshal.ReleaseComObject(application);
Now you are done with Excel, and have all of the different pieces of information you need stored as C# variables, and you can do with these what you wish.
Yes it is, and if you want to to that without Excel Interop you can use ClosedXML altough it works with excell 2007 + .
Try to use NPOI (http://npoi.codeplex.com/) or NetOffice (http://netoffice.codeplex.com/) library

Get the formatting of a cell in an Excel 2010 template

Im generating an Excel 2010 report based on a template in code using Microsoft.Office.Interop.Excel;
I need to grab the formatting from a cell in the template and apply it on subsequent cells down the column. To simplify, I want my cells to be right justified. Maybe its faster to specify this explicitly in code but I would prefer if I could base formatting on the template and not hardcode a particular style.
I am using workSheet.get_Range("L2", Type.Missing).get_Resize(1, 1) to select the cell.
Any suggestion greatly appreciated.
I'm not sure where get_Range() comes into the picture. I usually just get a Range directly.
I think what you are after is something to this effect (filler code there so you can see the variable names):
Dim oExcel As Object
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim AlignType As Long
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open("MySheet.xlsx")
oSheet = oBook.Worksheets(1)
AlignType = oSheet.Range("G1").HorizontalAlignment
oSheet.Range("G1:G" & oSheet.Range("G1").End(Excel.XlDirection.xlDown).Row).HorizontalAlignment = AlignType
Change the ranges to suit your own code.
Basically, read the value out (it's an enum, so you don't need to get the actual setting, just its number), and write it back into the other cells. You could probably do it all in one step, I separated it for clarity.

C# Opening csv string in Excel without saving the string

I have a string in csv format, that I wish to simply open in Excel, without first having to save the file.
Is this possible? I've had a look at Interop but I cannot find the specific method that I need.
Thanks.
If you convert your csv-string to a 2-dimensional array first, you can pass it to a range of cells.
Excel.Range oRange = oSheet.Range("A1",Missing.Value);
oRange.Resize(myArray.GetLength(0),myArray.GetLength(1));
oRange.Value = myArray;
This code is written out of my mind, so I hope It's ok, but I think you get the picture.
If you like to include a header in your excel-file, just start the range from A2 instaed of A1.
Yes, just copy the values over manually. You can find a sample here.
Adapting that sample to get a very short sample to get you started (please note, this sample is not complete and you need to read the complete sample to see how to do things properly):
Excel.Application oXL= new Excel.Application();
oXL.Visible = true;
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
oSheet.Cells[1, 1] = [first value from csv];
But assuming that it's a range of values in the csv string, just do a String.Split on it and then use the array to populate a Range in the sheet, just check out the sample in the article for details.

Best way reading from dirty excel sheets

I have to manipulate some Excel documents with C#. It's a batch process with no user interaction. It's going to parse data into a database, then output nice reports. The data is very dirty and cannot be ready using ADO. The data is nowhere near a nice table format.
Best is defined as the most stable(updates less likely to break)/ clear(succinct) code. Fast doesn't matter. If it runs in less than 8 hours I'm fine.
I have the logic to find the data worked out. All I need to make it run is basic cell navigation and getvalue type functions. Give me X cell value as string, if it matches Y value with levenshtein distance < 3, then give me Z cell value.
My question is, what is the best way to dig into the excel?
VSTO?
Excel Objects Library?
Third Option I'm not aware of?
VSTO is kind of a pain because of permissions and the fact that your dll becomes hooked to the document you're using. Assuming you're not actually changing the files, and ADO is definitely not an option, I would say that automation through the Excel COM interfaces is your best bet. It lets you program the way you normally would for any other application, and gives you just as many options for data extraction as VSTO.
The Office programs can be loaded as objects in .NET. The following is the coding stub that I used to load Excel into VB6. The code is essentially going to be the same regardless of which MS language you use.
Dim xlApp As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
On Error Resume Next
wb = xlApp.Workbooks.Open("c:\testdata.xls")
If Err.Number > 0 Then
If Err.Number = 1004 Then
MsgBox("File not found")
Else
MsgBox("Error " & Err.Number & " occurred.")
End If
Exit Sub
End If
ws = wb.Sheets("Sheet1")
Text1.Text = ws.Cells(1, 1).Value
wb = Nothing
ws = Nothing
xlApp = Nothing
Well try to see stack over flow question Convert Excel Range to ADO.NET DataSet or DataTable, etc

Categories

Resources