How to iterate through Excel only extracting data from specific columns - c#

I need to extract row data from specific columns in an xls file and add each row to a list as I iterate. I looked on this site and found How to iterate through Excel Worksheets only extracting data from specific columns, but I am not sure how to modify to allow adding to a list while going through each iteration.
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(#"C:\Temp\Sample\Sample.xls");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
//Not sure if I need to iterate this way or not
}
}

In the second loop, you want to add a conditional statement (If-then-else) code to check if you are at the column from which you want to extract your data.
In your conditional statement where your condition is met positively with the criteria for extraction, you want to insert your extraction code. For Example look at the following pseudo code:
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
If extraction condition
then
extraction code
else
keep looking code
end
}
}
BTW you may want to look into the "Look Up" function in Excel

Related

I want find duplicated values in my excel sheet and change the text colors in c#

I want find duplicated values in my excel sheet and change the text colors in c#
with this code:
Excel.Application xlApp =
new Excel.Application();
//xlApp.Visible = true;
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(DtaSource1);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for(int k=1;k<=rowCount;k++)
{
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j] == xlRange.Cells[k, 1])
{
xlRange.Cells[i, j].value = "00000";
xlWorksheet.SaveAs(DtaSource1);
}
}
}
}
but it's does not working and when i want to save changes it's throw out the error excel sheet read- only!
Here's an example of highlight duplicate and unique values in Excel by using Spire.XLS, maybe you can have a try.
//Load the Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Use conditional formatting to highlight duplicate values in range "A2:A10" with IndianRed color
ConditionalFormatWrapper format1 = sheet.Range["A2:A10"].ConditionalFormats.AddCondition();
format1.FormatType = ConditionalFormatType.DuplicateValues;
format1.BackColor = Color.IndianRed;
//Save the file
workbook.SaveToFile("HighlightDuplicates.xlsx", ExcelVersion.Version2013);

How can I copy specific cells(with Value,Color,Font) from one excel file to another excel file C#?

Here I have used this code from copying cells from one excel file to another. But it take lot of time.
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 10;j++) {
xlWorkSheet.Cells[i, j].style.Font.Color = ws.Cells[i, i].style.Font.Color;
xlWorkSheet.Cells[i, j].Interior.Color = ws.Cells[i, i].Interior.Color;
xlWorkSheet.Cells[i, j] = ws.Cells[i, i];
}
}
Is there any other method available?
Follow the documentation: Range.Copy method
If you would like to copy only text or formatting (etc.), then use PasteSpecial together with Copy method: Range.PasteSpecial
Usage:
//define cells (range) to copy
Excel.Range from = srcworkSheet.Range["A1:D100"];
//define destination range
Excel.Range to = destworkSheet.Range["A1"];
from.Copy(to);
//this should also works:
//xlWorkSheet.Cells[i, j].Copy(ws.Cells[i, j]);
Good Luck!

Issue select statement on excel sheet without using OLEDB provider

How can I use a select statement on an Excel sheet without using any oledb Provider? This is what I got so far by using using Excel = Microsoft.Office.Interop.Excel;. Iterating through each cell using a nested for-loop feels wrong...
// Create excel application object by calling constructor
Excel.Application xlApp = new Excel.Application();
// Open excel file using excel object
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(#"D:\Temp\file.xls");
// Open first sheet within excel document (index start at 1, not 0)
Excel._Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets["SheetName"];
// Get used sheet bounderies
Excel.Range xlRange = xlWorksheet.UsedRange;
// Get row count
int rowCount = xlRange.Rows.Count;
// Get column count
int colCount = xlRange.Columns.Count;
//iterate over the rows and columns and print to the console as it appears in the file
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
//new line
if (j == 1)
Console.Write("\r\n");
//write the value to the console if cell value ends on 'd'
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null && (xlRange.Cells[i, j].Value2.ToString()).EndsWith("d"))
Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
}
}
While you could use the items interface to a range to go over all the cells, you are outputting for each row and conditionally outputting across columns, for is the right approach.
However, you could tighten the code up a little:
var rowCount = xlRange.Rows.Count;
var colCount = xlRange.Columns.Count;
for (int row = 1; row <= rowCount; ++row) {
Console.WriteLine();
for (int col = 1; col <= colCount; ++col) {
//write the value to the console if cell value ends on 'd'
if (xlRange.Cells[row, col]?.Value2?.ToString().EndsWith("d") ?? false)
Console.Write(xlRange.Cells[row, col].Value2.ToString() + "\t");
}
}

Read xlsx file on C#.net

I want to read xlsx elements and print them 4 by 4 till all element are printed but i have a problem because it prints me all elements of the file. Any idea how to print them 4 by 4
// Open the Excel file.
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(Path.GetFullPath(#"C:\Users\user\Documents\Book2.xlsx"));
// Get the first worksheet.
Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets.get_Item(1);
// Get the range of cells which has data.
Excel.Range xlRange = xlWorksheet.UsedRange;
// Get an object array of all of the cells in the worksheet with their values.
object[,] valueArray = (object[,])xlRange.get_Value(
Excel.XlRangeValueDataType.xlRangeValueDefault);
for (int row = 1; row <= xlWorksheet.UsedRange.Rows.Count; ++row)
{
for (int col = 1; col <= xlWorksheet.UsedRange.Columns.Count; ++col)
{
Console.WriteLine(valueArray[row, col].ToString());
}
int y = xlWorksheet.UsedRange.Rows.Count / 4;
for() {
Console.WriteLine("4 addresat e para u printuan");
Console.WriteLine(" ");
}
}
Don't loop until you get to the end of columns/tows collection. Loop 1 through 4 (or 0 to 3)...
for (int row = 1; row <= 4; ++row)
{
for (int col = 1; col <= x4; ++col)
{
Console.WriteLine(valueArray[row, col].ToString());
}
}

Export a multidimensional string array to a new Excel spreadsheet

Using Interop I managed to import an excel document (that contains a population) to a multidimensional string array of this format:
public string[,] DataArray;
I used this method to populate the array:
try
{
Excel.Application xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(FilePath);
Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
rowCount = xlRange.Rows.Count;
colCount = xlRange.Columns.Count;
InitialiserTableauPopulation(rowCount, colCount);
for (int x = 1; x <= colCount; x++)
{
for (int y = 1; y <= rowCount; y++)
{
DataArray[x - 1, y - 1] = xlRange.Cells[y, x].Value2.ToString();
}
}
xlApp.Workbooks.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.message);
}
While debugging, I can see that the format is correct, for every X (column name), I have multiple Y (row data).
Example : DataArray[0,0] = NAME, DataArray[0,1] = JAMES, DataArray[0,2] = ERIC, etc.
Now what I'm trying to do is take a sample of this population, make a new multidimensional string array then export this "sample" to a new excel document : but I'm lost.
How should I proceed to export an existing two dimensional string array to a new excel sheet, keeping the same Array[column][row] format?
You just need to create a new Excel.Application, add a new Excel.Workbook and get the Excel.Worksheet from the Excel.Workbook.
Then you can iterate over your DataArray with a nested for-loop and add your values as needed using the Cell-Property of the Excel.Worksheet.
Then you save your new workbook and close it appropriately. This could look something like this
private void SaveDataArray(string excelFileName, string[,] dataArray)
{
var xlApp = new Application();
var xlWorkBook = xlApp.Workbooks.Add();
var xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.Item[1];
for (int i = 0; i < dataArray.GetLength(0); i++)
{
for (int j = 0; j < dataArray.GetLength(1); j++)
{
xlWorkSheet.Cells[j + 1, i + 1] = dataArray[i, j];
}
}
xlWorkBook.SaveAs(excelFileName);
xlWorkBook.Close(true);
xlApp.Quit();
}
And then call the method like this
// create some sample data ...
string[,] dataArray = new string[1, 3];
dataArray[0, 0] = "NAME";
dataArray[0, 1] = "JAMES";
dataArray[0, 2] = "ERIC";
SaveDataArray("c:\\temp\\exceltest.xlsx", dataArray);
While I dont have a great answer for you, you didnt exactly give us the interop you are using. First I would ask why you are trying to do this the hard way? Can you not use a 3rd part lib like http://epplus.codeplex.com/

Categories

Resources