改变excel文件中的颜色

我将改变一些Excel单元格的颜色,并使用下面的函数:

private void FormatFile(Excel.Borders _borders) { _borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous; _borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous; _borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous; _borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous; _borders.Color = ConsoleColor.Black; _range = _xlWorkSheet.get_Range("A1:AG100"); _range.EntireColumn.AutoFit(); _range.EntireRow.AutoFit(); Excel.Range colorRange; colorRange = _xlWorkSheet.get_Range("y1", "ag100"); colorRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray); } 

它工作正常,但当程序将保存该文件它来到这个屏幕:

在这里输入图像说明

你有一个想法如何改变,我不成为那个窗口?

我用这个函数保存文件:

  internal string SaveFile(string writePath) { FormatCells(_xlWorkSheet.Cells.Borders); string fileName = string.Format("{0}_{1}.xlsx","running_", DateTime.Now.ToString("yyyyMMdd_HHmmss")); _xlWorkBook.SaveAs(Path.Combine(writePath, fileName), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); _xlWorkBook.Close(true, misValue, misValue); _xlApp.Quit(); ReleaseObject(_xlWorkSheet); ReleaseObject(_xlWorkBook); ReleaseObject(_xlApp); return fileName; } 

将文件保存为新的格式xlsx而不是xls。

这里有更详细的介绍: 使用Microsoft.Office.Interop.Excel SaveAs Error导出到.xlsx

要以编程方式保存您需要使用的XLSX文件格式

 Excel.XlFileFormat.xlOpenXMLWorkbook 

作为文件格式。