creating simple excel sheet in c# with strings as input - c#

I am working on creating EXcel sheet in C#.
No Of columns:4
Name of columns: SNO, Name, ID, Address
There is no constarint on number of rows.
SNO Name ID Address
1 A 1122 XXXX
2 B 2211 YYYY
--- --- ---- ---
I have strings as input
string sno, string name, string Id, string address
I am actually new to C# background.
Can any one share their view on it like dlls needed etc.
Thank you

If you include a reference to Excel Interop you can do whatever you please having Office installed on your system.
A little example:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Excel.Application();
excel.Visible = true;
Excel.Workbook wb = excel.Workbooks.Open(excel_filename);
Excel.Worksheet sh = wb.Sheets.Add();
sh.Name = "TestSheet";
sh.Cells[1, "A"].Value2 = "SNO";
sh.Cells[2, "B"].Value2 = "A";
sh.Cells[2, "C"].Value2 = "1122";
wb.Close(true);
excel.Quit();

Related

Generate a spreadsheet with multiple worksheets from Microsoft C#

Existing C# code created in Visual Studio generates a complete report in the form of an Excel workbook with the report formatted in a single worksheet in that workbook. Need to create a workbook with a variable number of worksheets with the same report format but with different data values on each worksheet.
Current C# program creates a workbook with a single worksheet using the following code:
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value)); //Get a new workbook.
oSheet = (Excel._Worksheet)oWB.ActiveSheet; // add a worksheet
Tried using a "FOR" loop using the index of the FOR loop to name the worksheets. How can the worksheet reference ("oSheet") be indexed in the loop? Adding the FOR loop "ii" index to the oSheet name give a compiler error:
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value)); //Get a new workbook.
for (int ii = count; ii > 0; ii--)
{
oSheet[ii] = (Excel._Worksheet)oWB.ActiveSheet; // add a worksheet

C# and Excel, select column by name and get highest value

I'm quite new with c#, and with excel management too. I'm trying to do it with Microsoft.Office.Interop.Excel.
I have an excel file of this kind:
and what i want to do is open the file, read a column (i.e. x-com column) and get the highest number (in case of numbers, of course).
I opened the file with
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(fileName);
Excel.Worksheet xlWorksheet = xlWorkBook.Sheets[sheetNumber];
Excel.Range xlRange = xlWorksheet.UsedRange;
fileName and sheetNumber are the inputs of this method.
how can i read only one column? and get the highest number?
[optional] I am not sure what exactly is an Excel.Range. how can I print it on the console? what would look like the print if I print xlRange?
Edit:
I used the following libraryes:
- using ExcelDataReader;
- using ClosedXML.Excel;
I don't really remember wich of the two libraryes I used but It probably is the second one.
You can try this code:
var file = new XLWorkbook("YOUR EXCEL FILE PATH");
var foglio = file.Worksheet("sheet"); //select the worksheet
var firstRowUsed = foglio.FirstRowUsed(); //Gets the first row used
var ComuniRow = firstRowUsed.RowUsed(); //Gets the very first row used (in your case A1:"Control System")
var LastRow = LastRowUsed.RowUsed(); //Last Row used
ComuniRow = ComuniRow.RowBelow();//Select the row below (in your case A2:"P/M").
string Value = ComuniRow.GetString(); //Assigns "P/M" to the string
Now you have the exact position of a field.
At this point you just have to cycle the whole column till the "LastRowUsed".
while(ComuniRow != LastRow)
{
string test = ComuniRow.GetString();//the value of the current selected cell
ComuniRow = ComuniRow.RowBelow();
}
If you need to select a specific Column you can use .Cell(x)
while(ComuniRow.Cell([cell value]) != LastRow.Cell([cell value]))
{
string test = ComuniRow.Cell([cell value]).GetString();//the value of the current selected cell
ComuniRow = ComuniRow.RowBelow();
}

C# - Transpose excel table using Interop

I should transpose a table of an excel sheet in c#. There is a method to do it into the Microsoft.Office.Interop.Excel api, or any other method to do this?
Edit
This is my code:
string pathFile, range;
pathFile = #"C:\Users\Administrator\Desktop\" + fileName;
Excel.Application app = new Excel.Application();
Excel.Workbook book = app.Workbooks.Open(pathFile);
Console.WriteLine("Inserire il range delle celle da copiare(sono tutti uguali): ");
range = Console.ReadLine();
Excel.Range rgn = app.get_Range(range);
Object[,] transposeRange = (Object[,])app.WorksheetFunction.Transpose(rgn);
transposeRange = app.ActiveSheet.Range("A1").Resize(transposeRange.GetUpperBound(0), transposeRange.GetUpperBound(1));
Transpose method exists in the Microsoft.Office.Interop.Excel library. You may see the usage of it at the below link :
C# Transpose() method to transpose rows and columns in excel sheet

Excel worksheets error: One or more types required to compile a dynamic expression cannot be found

I am trying to get the Excel sheet cell values to be used as inputs in a test script.
I have been following the http://www.telerik.com/forums/how-to-pass-excel-worksheet-name-path-as-string
string myPath = #"C:\Users\Monica\Desktop\ExcelTest.xlsx";
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Open(myPath);
//select the correct worksheet
Excel.Worksheet demoWorksheet = (Excel.Worksheet)excelApp.Worksheets["Sheet1"];
I am trying to select the worksheet "Sheet1" from Workbook "ExcelTest.xlsx" and getting the following error
"One or more types required to compile a dynamic expression cannot be found".
Try removing the "Microsoft.CSharp" reference then add it back your project.

How to only read cells with values with in a specific range in Excel - using VSTO C#

I am doing an ExcelAddin in VisualStudio 2010 for Excel 2007. In my Excel Workbook I have a named range that I call MyRange. It goes from cells C10 to M25. How can I read only the cells that have value in them within MyRange. Note I don´t want to read anything from the other cells, only within MyRange? I want to read the cells that have values in them into a Word document. I think I have that figured out.
I have tried to use UsedRange but that selects everything from A1-M25 ( I only want to select the cells with value from C10-M25). Here is what I got so far.
string FileName = #"C:\MyFile.xlsx";
Excel.Application xlApp = xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
xlWorkBook = xlApp.Workbooks.Open(FileName);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
object cell1 = "C10", cell2 ="M25";
//Here are some different versions that I tried. I also tried to use the SpecialCell //method but it didn´t seem to work.
Excel.Range namedRange = (Excel.Range)xlWorkSheet.get_Range("C10", "M25");
Excel.Range last = (Excel.Range)xlWorkSheet.UsedRange;
Excel.Range usedRange = (Excel.Range)xlWorkSheet.get_Range("C10", last);
Any help is highly appreciated. Thank you.
To read each cell value from your 'namedRange' you can loop through the range like this:
foreach (Excel.Range cell in namedRange.Cells)
{
if (String.IsNullOrEmpty(cell.Value.ToString()))
{
string thisCellHasContent = cell.Value.ToString();
string thisCellAddress = cell.Address.ToString();
}
}

Categories