C# Transpose() 方法来转置excel表格中的行和列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12838403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:38:43  来源:igfitidea点击:

C# Transpose() method to transpose rows and columns in excel sheet

c#.netexcelc#-4.0

提问by Shami

I want to transpose rows and columns of an excel sheet and save it. I want to use C# transpose() method for this purpose. Can anybody tell how it can be used in C# with examples, what is the syntax, what object shoud be created to call transpose() method?

我想转置 Excel 工作表的行和列并保存。我想为此使用 C# transpose() 方法。任何人都可以通过示例告诉它如何在 C# 中使用,语法是什么,应该创建什么对象来调用 transpose() 方法?

Thank you.

谢谢你。

采纳答案by InContext

your method is part of the Excel object model but included in C# through Visual Studio Tools for Office / Excel DNA / Managed XLL etc

您的方法是 Excel 对象模型的一部分,但通过 Visual Studio Tools for Office / Excel DNA / Managed XLL 等包含在 C# 中

Object[,] transposedRange = (Object[,])xlApp.WorksheetFunction.Transpose(rng.Value2);

Then paste the transposedRange back into Excel:

然后将 transposedRange 粘贴回 Excel:

xlApp.ActiveSheet.Range("A1").Resize(transposedRange.GetUpperBound(0), transposedRange.GetUpperBound(1)) = transposedRange;

回答by Shami

First you have to convert excel sheet to datatable and then programmatically transpose rows and columns of datatable.For converting excel sheet to datatable,you can search question on stackoverflow as it has lot of similar questions.For trasposing datatable below code works;

首先,您必须将excel表转换为数据表,然后以编程方式转置数据表的行和列。对于将excel表转换为数据表,您可以在stackoverflow上搜索问题,因为它有很多类似的问题。对于下面的代码工作转换数据表;

private DataTable GenerateTransposedTable(DataTable inputTable)
{
     DataTable outputTable = new DataTable();

     // Add columns by looping rows

     // Header row's first column is same as in inputTable
     outputTable.Columns.Add(inputTable.Columns[0].ColumnName.ToString());

     // Header row's second column onwards, 'inputTable's first column taken
     foreach (DataRow inRow in inputTable.Rows)
     {
         string newColName = inRow[0].ToString();
         outputTable.Columns.Add(newColName);
     }

     // Add rows by looping columns        
     for (int rCount = 1; rCount <= inputTable.Columns.Count - 1; rCount++)
     {
         DataRow newRow = outputTable.NewRow();

         // First column is inputTable's Header row's second column
         newRow[0] = inputTable.Columns[rCount].ColumnName.ToString();
         for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
         {
             string colValue = inputTable.Rows[cCount][rCount].ToString();
             newRow[cCount + 1] = colValue;
         }
         outputTable.Rows.Add(newRow);
     }

     return outputTable;
}

回答by Badda_Bing

Transpose can be accessed as a parameter of PasteSpecial(Transpose: whatever).
I have answered this in full here: Transpose values in excel using C#

转置可以作为 PasteSpecial(Transpose: whatever)的参数访问。
我在这里完整地回答了这个问题: Transpose values in excel using C#