Reading from Excel dynamically in C# - c#

I'm not sure if using the word "dynamic" is correct. Anyway, I do have some basic understanding of using the Microsoft.Office.Interop.Excel. The problem is, I'm having about 100 excel files in a folder, each of the excel files has different sheet name, number of rows and number of columns.
As far as I understand, you need to specify the range and sheet name, i.e.:
xcel.Worksheet sheet = someExcelFiles.Sheets["SomeSheetName"] as Excel.Worksheet;
Excel.Range range = sheet.get_Range("A1:A5");
Is there anyway so that my application can read all data in all of the excel files without having to specify the sheet name and range (row and columns)?

Short answer yes. Long answer From DotNetPerls which also contains grabbing number of sheets programatically.
Range excelRange = sheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);

Related

Merging excel cells based on the cell value using c#

I am using excel with c# I want to merge all adjacent cells that contain a specific value using c# code.
I want to merge all the cells that contains the value (Merged cells) using c# code
i want it to be like this
First, you need to identify the cells where the range of repetitive value starts and where it ends.
Then, use this code to merge the cells:
String startRange = "A1";
String endRange = "D3";
String repetitiveValue = "Merged Cells";
Microsoft.Office.Interop.Excel.Range range = worksheet.Range[startRange, endRange];
range.Value2 = repetitiveValue;
range.Select();
range.Merge(Missing.Value);
You need to use one or the other Office library, which can help you with reading/writing spreadsheets & also with the cell merges.
Check SpreadSheetGear.net or PowerTools for OpenXml (for >= office 2007 formats)
http://ericwhite.com/blog/powertools-for-open-xml-expanded/

Adding a row into a sheet using NetOffice

I have a
Excel.Workbook book = application.Workbooks.Open(txtPathExcel.Text);
Excel.Worksheet mainSheet = (Excel.Worksheet)book.Sheets[1];
How can i add a new row in mainSheet? I can't find any NetOffice functions for it.
All rows are already exist in the sheet, you just need to select the required.
The Rows property of the Application class returns a Range object that represents all the rows on the active worksheet. For example:
Worksheets("Sheet1").Rows(3)
The range object refers to the third row on the sheet.
Also you may find the Insert method helpful. It inserts a cell or a range of cells into the worksheet or macro sheet and shifts other cells away to make space.

Copy excel rows programatically with column widths

hope someone can help. i have an excel workbook and in that i have several sheets.
what i want to do is get row 1 from sheet named "Sheet1" and paste it into several other sheets (not all). Also, i want to maintain the column widths of row 1 from sheet 1 while copying, this is actually very important.
i mention several other sheets because i would like exclude some sheets, which i am planning to store in a list.
Microsoft.Office.Interop.Excel.Application xlApp = null;
Excel.Workbook xlWorkbook = null;
Excel.Sheets xlSheets = null;
Excel.Worksheet xlNewSheet = null;
any ideas how this can be done in c#?
thanks in advance.
Maintaining the width of a column or a row is very difficult to do. In my opinion: first copy the data, then read the width values of the old columns and apply them to the new columns.
All other solutions which I have seen rely on pastespecial and sometimes do not work.

Adding a new column at the start of an excel table in an excel

I am wondering how one can insert a new column at the beginning of an excel spreadsheet using C sharp.net. I am using Microsoft.Office.Interop.Excel; I want to use OLEO to do this, but I don't see that happening. I have been searching google on how to do this for the last two days now. I can't understand why there are not more tutorials on this?
Checkout out the Range.Insert method which you can call on a range of cells to insert entire rows/columns in front of them:
Worksheet sheet = (Worksheet) workBookIn.Sheets[1]; // Worksheet indexes are one based
Range rng = sheet.get_Range("A1", Missing.Value);
rng.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);

How do I create an Excel range object that refers to a name in Excel (c#)

I have an excel worksheet with various names and I want to create an object that references that name. The code I am working with uses the cell address, but that is very cumbersome, and I would rather use the names.
My code looks like this right now:
myWorksheet= GetWorkSheet(myWorkbook);
Excel.Range range = myWorksheet.get_Range(cell1, cell2);
Instead of using cell1 and cell2, I would like to use the name that is in Excel already that describes the range.
Thanks for your help.
Simply do:
Excel.Range range = myWorksheet.get_Range("MyName", Type.Missing);

Categories

Resources