C# - Transpose excel table using Interop - c#

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

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

How do I copy last column value of an excel sheet to another Excel sheet's last column using asp.net c#?

This is the screenshot of the last column in an Excel sheet that I want to copy to another Excel sheet last column
May I know how to do it using asp.net c# code?
You can use sheet.Copy() to copy a range from an excel sheet to another excel sheet.
Workbook workbook1 = new Workbook();
workbook1.LoadFromFile("file1.xlsx");
Worksheet sheet1 = workbook1.Worksheets[0];
Workbook workbook2 = new Workbook();
workbook2.LoadFromFile("file2.xlsx");
Worksheet sheet2 = workbook2.Worksheets[0];
//copy the last column of sheet1 and insert after the last column of sheet2
sheet2.Copy(sheet1.Range[sheet1.FirstRow, sheet1.LastColumn, sheet1.LastRow, sheet1.LastColumn], sheet2.Range[sheet2.FirstRow, sheet2.LastColumn + 1, sheet2.LastRow, sheet2.LastColumn + 1], true);
workbook2.SaveToFile("copy.xlsx", ExcelVersion.Version2013);

How to merge 3 Sheets from different Excel files into one sheet with C#

Here I tried to merge two excel files into one excel sheet using below mentioned code (Spire.Xls dll) its working fine.
Here is code for two excel merging.
workbook = new Workbook();
//load the first workbook
workbook.LoadFromFile(ArrayExcelFiles[0]);
//load the second workbook
Workbook workbook2 = new Workbook();
workbook2.LoadFromFile(ArrayExcelFiles[1]);
//import the second workbook's worksheet into the first workbook using a datatable
Worksheet sheet2 = workbook2.Worksheets[0];
DataTable dataTable = sheet2.ExportDataTable();
Worksheet sheet1 = workbook.Worksheets[0];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
workbook.SaveToFile(OutputPath + "Merged.xls", ExcelVersion.Version2007);
But the problem is that when am trying to merge three excel files using same logic the output was same as two merged excel output third excel is not merged.
Here is the code for three excel files.
Workbook workbook1 = new Workbook();
//load the first workbook
workbook1.LoadFromFile(ArrayExcelFiles[0]);
//load the second workbook
Workbook workbook2 = new Workbook();
workbook2.LoadFromFile(ArrayExcelFiles[1]);
//load the third workbook
Workbook workbook3 = new Workbook();
workbook3.LoadFromFile(ArrayExcelFiles[2]);
//import the second workbook's worksheet into the first workbook using a datatable
Worksheet sheet3 = workbook3.Worksheets[0];
DataTable dataTable = sheet3.ExportDataTable();
Worksheet sheet2 = workbook2.Worksheets[0];
dataTable = sheet2.ExportDataTable();
Worksheet sheet1 = workbook1.Worksheets[0];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
workbook1.SaveToFile(OutputPath + "Merged.xls", ExcelVersion.Version2007);
You are not using the same logic in the second example, thus it does not work. Try something like this on the second code or change it a bit:
//import the second workbook's worksheet into the first workbook using a datatable
Worksheet sheet3 = workbook3.Worksheets[0];
Worksheet sheet2 = workbook2.Worksheets[0];
DataTable dataTable2 = sheet2.ExportDataTable();
Worksheet sheet1 = workbook1.Worksheets[0];
DataTable dataTable1 = sheet1.ExportDataTable();
sheet3.InsertDataTable(dataTable2, false, sheet3.LastRow + 1, 1)
sheet3.InsertDataTable(dataTable1, false, sheet3.LastRow + 1, 1)
The code above takes sheet3 and inserts 2 data tables to it - one from sheet2 and one from sheet1.
You only ever insert the sheet from workbook2, so it's not surprising. You got datatable from workbook3, but then immediately over-wrote it with data from workbook2, before you did anything else with it. You can't store two tables in one DataTable object. The last one simply replaces the first one (just like any other variable assignment).
You need to run two separate insertDataTable commands with two separate data tables, one from each sheet:
//import the second and third workbooks' worksheets into the first workbook using a datatable
Worksheet sheet2 = workbook2.Worksheets[0];
DataTable dataTable = sheet2.ExportDataTable();
Worksheet sheet3 = workbook3.Worksheets[0];
DataTable dataTable2 = sheet3.ExportDataTable();
Worksheet sheet1 = workbook1.Worksheets[0];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
sheet1.InsertDataTable(dataTable2, false, sheet1.LastRow + 1, 1);
workbook1.SaveToFile(OutputPath + "Merged.xls", ExcelVersion.Version2007);
Of course the code could be made a lot neater and less repetitive by using loops etc, but this is the basic solution.

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();
}
}

creating simple excel sheet in c# with strings as input

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();

Categories