问题
I saw this link - C# Transpose() method to transpose rows and columns in excel sheet in stackoverflow and this is what I am trying to do. But the guy is pretty unhelpful in the answers as he does not provide the full information needed. I am simply wanting to transpose cells A9:B15 in my excel sheet and then copy them either into a new xls file, a new worksheet, or better yet delete the current worksheet contents and replace it with the newly transposed paste contents. Clearly it can be done through the WorksheetFunction.Transpose method but I can't seem to get it to work as I don't know what rng or value2 are? I could create a datatable but surly using this method seems a more appropriate way of doing it. Here is the code from the stackoverflow question. .
Object[,] transposedRange = (Object[,])xlApp.WorksheetFunction.Transpose(rng.Value2);
xlApp.ActiveSheet.Range("A1").Resize(transposedRange.GetUpperBound(0), transposedRange.GetUpperBound(1)) = transposedRange;
Here is my code so far:
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(@"P:\Visual Studio 2013\Projects\Debugging\Debugging\test.htm");
Microsoft.Office.Interop.Excel.Range rng = excel.get_Range("A9:B15");
Object[,] transposeRange = (Object[,])excel.WorksheetFunction.Transpose(rng);
transposeRange = excel.ActiveSheet.Range("A1").Resize(transposeRange.GetUpperBound(0), transposeRange.GetUpperBound(1));
wb.SaveAs(@"P:\Visual Studio 2013\Projects\Debugging\Debugging\testing.xls");
Not sure if I have done the rng right. I am so confused by this.
回答1:
Seems like nobody actually bothered to answer this and its still the top search engine hit for this issue (@ July 2019, go figure...!), so here's my 2 cents... I did not understand the hype about the WorksheetFunction.Transpose method. "Objectifying" things around isn't perhaps the cleanest way to go about this, particularly when using the Excel Interop anyway. At the end of the day, Transpose has been a dynamic parameter of the PasteSpecial() method since time immemorial. So why not use it as such? I think this was what prompted some people to suggest using VBA instead of C#... Anyway, this code works and does what the question requires methinks:
First get the references right...
using System;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
Then try this...
string filePath = @"P:\Visual Studio 2013\Projects\Debugging\Debugging\test.htm";
string savePath = @"P:\Visual Studio 2013\Projects\Debugging\Debugging\testing.xls";
var excelApp = new Excel.Application()
{
Visible = true //This is optional
};
Workbooks workbook = excelApp.Workbooks;
workbook.Open(filePath);
Range range = excelApp.get_Range("A9:B15");
range.Copy();
excelApp.ActiveSheet.Range("A1").PasteSpecial(Transpose: true); //voila... :)
range.Delete(XlDeleteShiftDirection.xlShiftToLeft); //delete original range
if (!System.IO.File.Exists(savePath)) //is the workbook already saved?
{
excelApp.ActiveWorkbook.SaveAs(savePath); //save
}
else
{
Console.WriteLine("File \"{0}\" already exists.", savePath); //or do whatever...
Console.ReadLine();
return;
}
It could be simplified further... but it is more readable like this.
回答2:
Is there some reason you need to do this in C#?
If what you want is just what you state, VBA code can accomplish this also. Just
- read the transposed range into a variant array
- clear the worksheet
- write the variant array back to the worksheet at the cell of your choice.
- Note that when you write the variant array back to the worksheet, the destination range must be the same size as the array.
Option Explicit
Sub TransposeRange()
Dim RGtoTranspose As Range
Dim V As Variant
Set RGtoTranspose = Range("A9:B15")
V = WorksheetFunction.Transpose(RGtoTranspose)
Cells.Clear
Range("a1").Resize(UBound(V, 1), UBound(V, 2)).Value = V
End Sub
来源:https://stackoverflow.com/questions/27852602/transpose-values-in-excel-using-c-sharp