我正在尝试使用 Microsoft Interop Excel 对象规范化 Excel 电子表格中的数据。基本上,我需要将列转换为从某个列偏移量开始的行。
Original Data:
ColumnA ColumnB ColumnC ColumnD ColumnE ColumnF
X Y 10 20 30 40
Normalized Data:
ColumnA ColumnB NewColumn Value
X Y ColumnC 10
X Y ColumnD 20
X Y ColumnE 30
X Y ColumnF 40
我的功能按预期工作。但是,运行时间非常缓慢。所以,我想知道如果我使用像 OpenXML 这样的任何其他框架,我会看到效率的提高吗
这是我使用互操作对象的代码:
public static void Normalize(string aFilePathName, string aSheetName, int aColOffSet, string aPivotColName, string aValueColName)
{
LOG.DebugFormat("Normaling data in file: {0}", aFilePathName);
LOG.DebugFormat("Sheet Name:{0} ColOffset:{1}", aSheetName, aColOffSet);
Excel.Application vExcel = new Excel.Application();
Excel.Workbook vWorkbook = null;
Excel.Worksheet vWsOriginal = null;
Excel.Worksheet vWsNormalized = null;
try
{
vExcel.Visible = false;
vWorkbook = vExcel.Workbooks.Open(aFilePathName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
vWsOriginal = vWorkbook.Worksheets[aSheetName];
string vNormalizedSheetName = string.Format("Normalized {0}", aSheetName);
bool vNormalizedSheetExists = (vWorkbook.Sheets.Cast<object>()
.Select(sheetValue => sheetValue as Excel.Worksheet))
.Any(wbSheet => wbSheet != null && wbSheet.Name == vNormalizedSheetName);
if (!vNormalizedSheetExists)
{
vWsNormalized = vWorkbook.Worksheets.Add(vWsOriginal, Type.Missing, Type.Missing, Type.Missing);
vWsNormalized.Name = vNormalizedSheetName;
}
else
{
vWsNormalized = vWorkbook.Worksheets[vNormalizedSheetName];
}
vWsNormalized.UsedRange.ClearContents();
long vTotalColumns = 1;
long vRowCounter = 1;
Excel.Range vWsRange = vWsOriginal.Cells[vRowCounter, vTotalColumns];
List<string> vHeaders = new List<string>();
while (vWsRange.Value2 != null)
{
vHeaders.Add(vWsRange.Value2.ToString());
vTotalColumns = vTotalColumns + 1;
vWsRange = vWsOriginal.Cells[vRowCounter, vTotalColumns];
}
// Insert the headers
for (int vHeaderCol = 1; vHeaderCol < aColOffSet; vHeaderCol++)
{
vWsNormalized.Cells[1, vHeaderCol].Value = vHeaders[vHeaderCol - 1];
}
vWsNormalized.Cells[1, aColOffSet].Value = aPivotColName;
vWsNormalized.Cells[1, aColOffSet + 1].Value = aValueColName;
long vNewRow = 2;
for (int vCol = aColOffSet; vCol < vTotalColumns; vCol++)
{
vRowCounter = 2;
while (((Excel.Range)vWsOriginal.Cells[vRowCounter, 1]).Value2 != null)
{
for (int j = 1; j < aColOffSet; j++)
{
vWsNormalized.Cells[vNewRow, j] = vWsOriginal.Cells[vRowCounter, j];
}
vWsNormalized.Cells[vNewRow, aColOffSet] = vWsOriginal.Cells[1, vCol];
vWsNormalized.Cells[vNewRow, aColOffSet + 1] = vWsOriginal.Cells[vRowCounter, vCol];
vRowCounter = vRowCounter + 1;
vNewRow = vNewRow + 1;
}
}
}
finally
{
vWorkbook.Close(Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(vWsNormalized);
Marshal.FinalReleaseComObject(vWsOriginal);
Marshal.FinalReleaseComObject(vWorkbook);
vExcel.Quit();
Marshal.FinalReleaseComObject(vExcel);
}
}
如果有可能提高性能,我愿意尝试任何其他开源框架。
谢谢