我有一个文本文件,我试图保存为XLSX文件。但是在保存和关闭之前我需要执行TextToColumns,而且我需要列“H”以保持“文本”格式。
下面的代码将“H”列转换为“Text”格式,但是当“.TextToColumns”执行时,“H”列变为“自定义”格式,从而更改数据。
Microsoft.Office.Interop.Excel.Application oXL;
Microsoft.Office.Interop.Excel.Workbook dataB;
Microsoft.Office.Interop.Excel.Worksheet dataS;
string path = @"C:\Users\fakename\Desktop\EXTRACTED_FORMATTED sampleFile.csv";
oXL = new Microsoft.Office.Interop.Excel.Application();
dataB = oXL.Workbooks.Open(path, 0,
false, 5, Missing.Value, Missing.Value, false, Missing.Value, Missing.Value,
false, false, Missing.Value, false, false, false);
dataB.Application.DisplayAlerts = false;
oXL.Visible = true;
dataS = (Microsoft.Office.Interop.Excel.Worksheet)dataB.ActiveSheet;
Range excelRange = dataS.get_Range("H1").EntireColumn;
excelRange.NumberFormat = "@";
dataS.get_Range("A1", ("A" + dataS.UsedRange.Rows.Count)).TextToColumns(
Type.Missing, Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,
Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, false,
false, false, false, false, true, "|", Type.Missing, Type.Missing, Type.Missing, Type.Missing);
dataS.Name = "PSH";
dataS.SaveAs(@"C:\Users\fakename\Desktop\2\moresamplefiles", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault);
dataB.Close(Missing.Value, Missing.Value, Missing.Value);
oXL.Quit();