【发布时间】:2016-01-05 20:55:37
【问题描述】:
我将更改一些 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;
}
【问题讨论】:
-
也许你可以在 2010 或 2013 等较新的 excel 中做到这一点
-
不使用 Office 自动化,而是使用 Open XML SDK 或类似 EPPlus 的库直接创建实际的
xlsx文件。这样您就不需要在用户的计算机上安装 Excel。 -
至于您的问题,请使用
xlOpenXMLWorkbook而不是xlWorkbookNormal,这适用于xls文件