我仍然停留在这段代码中,并且错误不断出现。我有将近 24 列的 excel 文件,我正在尝试将其调整为 7 列的 excel 文件,因为我拥有的软件仅适用于 7 列而我没有不想从头重新编写软件,所以你会看到一些列的删除和插入。
我在这个 excel 文件中有 5 列具有“x”值或空值。
我要做的是在范围 A1 和 B1 之间创建一个称为类别的新列,所以如果第 5 列中有 x,我在类别字段中写 E,否则如果 x 在第 6 列然后我在类别列中写 P.. 等等。然后我需要删除我不再需要的这 5 列(范围 E1:I1)
问题是当我调试代码时,我可以看到 values[,] 插入了列,并且值已正确传输,但是当 temp_data.csv 生成时,删除后它有新的 excel 文件,所以现在它包含 11 列,但新的 Category 列以及值不存在...
Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(p_sUBKPath, Type.Missing, false, 4, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;
// delete columns that we don't need from the new excel file
Microsoft.Office.Interop.Excel.Range range2 = ws.get_Range("A1","A1");
range2.EntireColumn.Delete();
Microsoft.Office.Interop.Excel.Range range3 = ws.get_Range("B1", "B1");
range3.EntireColumn.Delete();
Microsoft.Office.Interop.Excel.Range range4 = ws.get_Range("D1", "L1");
range4.EntireColumn.Delete();
Microsoft.Office.Interop.Excel.Range range5 = ws.get_Range("I1", "M1");
range5.EntireColumn.Delete();
Microsoft.Office.Interop.Excel.Range range6 = ws.get_Range("K1", "K1");
range6.EntireColumn.Delete();
//insert a new column ( Category)
Microsoft.Office.Interop.Excel.Range range7 = ws.get_Range("B1", "B1");
range7.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight);
object[,] tempVal = (object[,])range.Value2;
tempVal[1, 2] = (object)"Category";
for (int row = 2; row <= tempVal.GetUpperBound(0); row++)
{
try
{
if ((!String.IsNullOrEmpty((string)tempVal[row, 5])) && (string)tempVal[row, 5] == "x")
{
tempVal[row, 2] = (string)"E";
}
else if ((!String.IsNullOrEmpty((string)tempVal[row, 6])) && (string)tempVal[row, 6] == "x")
{
tempVal[row, 2] = (string)"P";
}
else if ((!String.IsNullOrEmpty((string)tempVal[row, 7])) && (string)tempVal[row, 7] == "x")
{
tempVal[row, 2] = (string)"Phy";
}
else if ((!String.IsNullOrEmpty((string)tempVal[row, 8])) && (string)tempVal[row, 8] == "x")
{
tempVal[row, 2] = (string)"L";
}
else if ((!String.IsNullOrEmpty(tempVal[row, 9].ToString())) && (string)tempVal[row, 9] == "x")
{
tempVal[row, 2] = (string)"Ex";
}
else
MessageBox.Show("unknow");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
object[,] values = tempVal;
if (Convert.ToString(values[1, 1]).ToUpper().Trim() == "SHORT NAME" && Convert.ToString(values[1, 2]).ToUpper().Trim() == "CATEGORY" && Convert.ToString(values[1, 3]).ToUpper().Trim() == "LONG NAME EN" && Convert.ToString(values[1, 4]).ToUpper().Trim() == "LONG NAME DE" && Convert.ToString(values[1, 5]).ToUpper().Trim() == "ELEMENT" && Convert.ToString(values[1, 6]).ToUpper().Trim() == "PROPERNAME" && Convert.ToString(values[1, 7]).ToUpper().Trim() == "PHYSICAL" && Convert.ToString(values[1, 8]).ToUpper().Trim() == "LOGICAL" && Convert.ToString(values[1, 9]).ToUpper().Trim() == "EXTENSION" && Convert.ToString(values[1, 10]).ToUpper().Trim() == "CREATED BY" && Convert.ToString(values[1, 11]).ToUpper().Trim() == "CREATED ON" && Convert.ToString(values[1, 12]).ToUpper().Trim() == "STATE")
{
for (int row = 1; row <= values.GetUpperBound(0); row++)
for (int col = 1; col <= values.GetUpperBound(1); col++)
{
string value = Convert.ToString(values[row, col]);
if (value.Contains(","))
{
range.Cells.set_Item(row, col, value.Replace(",", p_sPsuedostring));
}
if (value.Contains(" "))
{
range.Cells.set_Item(row, col, value.Replace(" ", p_sPsuedostring + " " + p_sPsuedostring));
}
}
if (File.Exists(System.Windows.Forms.Application.StartupPath + @"\Data_Temp.csv"))
File.Delete(System.Windows.Forms.Application.StartupPath + @"\Data_Temp.csv");
//Save the Latest databse as Data_Temp.csv
ws.SaveAs(System.Windows.Forms.Application.StartupPath + @"\Data_Temp.csv", Microsoft.Office.Interop.Excel.XlFileFormat.xlUnicodeText, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xl.DisplayAlerts = true;
try
{
xl.Workbooks[1].Close(false, Type.Missing, Type.Missing);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
xl.Application.Quit();
xl.Quit();
l_bClosedSuccessfully = true;