Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
903 views
in Technique[技术] by (71.8m points)

c# - How can I convert a boxed two-dimensional array to a two-dimensional string array in one step?

Is there a way to convert a boxed two-dimensional array to a two-dimensional string array in one step using C#/.NET Framework 4.0?

using ( MSExcel.Application app = MSExcel.Application.CreateApplication() ) {
    MSExcel.Workbook book1 = app.Workbooks.Open( this.txtOpen_FilePath.Text );
    MSExcel.Worksheet sheet1 = (MSExcel.Worksheet)book1.Worksheets[1];
    MSExcel.Range range = sheet1.GetRange( "A1", "F13" );
    object value = range.Value; //the value is boxed two-dimensional array
}

I'm hopeful that some form of Array.ConvertAll might be made to work but so far the answer has eluded me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can write your own ConvertAll for two-dimensional arrays:

public static TOutput[,] ConvertAll<TInput, TOutput>(
    this TInput[,] array, Func<TInput, TOutput> converter)
{
    int length0 = array.GetLength(0);
    int length1 = array.GetLength(1);

    var result = new TOutput[length0, length1];

    for (int i = 0; i < length0; i++)
        for (int j = 0; j < length1; j++)
            result[i, j] = converter(array[i, j]);

    return result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...