I'm using c# 4.0 and am reading from one excel sheet and getting the values from each column then on another sheet i am writing just the distinct values per column.
Ie if results sheet has:
COLA COLB COLC
----- ------- -------
dog cow rabbit
cat moose hare
dog moose bunny
bird yak hare
fish fox
bunny
weasel
fox
bunny
then distinct sheet should have (ordering within each column is not important):
COLA COLB COLC
----- ------- -------
dog cow rabbit
cat moose hare
bird yak weasel
fish fox
bunny
Can anyone suggest a simpler way (still using com interop excel) that negates the need to use this ugly combination of 2d object array, arraylist, list back to array.etc.
This is my work in progress code, feels like I am going in the wrong direction:
for (int c = 0; c < num_of_cols; c++)
{
string colref = NumToLetter(c + 1);
int lastRow = workbook.Sheets["Results"].Cells[workbook.Sheets["Results"].Rows.Count, colref].End(MSExcel.XlDirection.xlUp).Row;
MSExcel.Range excelRange = workbook.Sheets["Results"].Range[colref + "2:" + colref + lastRow];
object[,] valueArray = (object[,])excelRange.get_Value(MSExcel.XlRangeValueDataType.xlRangeValueDefault);
List<ArrayList> distinctList = new List<ArrayList>();
for (int index = 0; index < num_of_cols.Length; index++)
distinctList.Add(new ArrayList());
for (int i = 1; i <= valueArray.GetUpperBound(0); i++)
{
if (valueArray[i, 1] != null)
{
if (!distinctList[c].Contains(valueArray[i, 1].ToString()))
distinctList[c].Add(valueArray[i, 1].ToString());
}
}
var startCell = (MSExcel.Range)distinctVals_worksheet.Cells[1, c + 1];
var endCell = (MSExcel.Range)distinctVals_worksheet.Cells[distinctList[c].Count,c + 1];
var writeRange = distinctVals_worksheet.Range[startCell, endCell];
writeRange.Value2 = distinctList[c].ToArray();
}
I think you can do this on a per column basis and use linq to select distinct values and then write them on the target. Something like this :
static IEnumerable<string> GetValuesOfColumn(this Excel.Sheet sheet,int col)
{
//returns all the values of the specific column of a sheet
}
static void WriteValuesToColumn(this Excel.Sheet sheet,IEnumerable<string> values,int col,int row)
{
//Writes values to a certain range of a sheet
}
then you can do something like this :
var distinctValues=mySheet.GetValuesOfColumn(0).Distinct();
myOtherSheet.WriteValuesToColumn(distinctValues,0,0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.