Excel file C# WinForms



  • We need to use the Excel file.
    I'.

    using Excel = Microsoft.Office.Interop.Excel;
    

    Then:

    private void writingInExcMethod() {
        try {
            Excel.Application ObjExcel = new Excel.Application();
            Excel.Workbook ObjWorkBook;
            Excel.Worksheet ObjWorkSheet;
            ObjWorkBook = ObjExcel.Workbooks.Add(System.Reflection.Missing.Value);
            ObjWorkSheet = (Excel.Worksheet)ObjWorkBook.Sheets[1];
            ObjWorkSheet.Cells[3, 1] = "51";
            ObjWorkBook.SaveAs("log.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            /**/
            ObjExcel.Quit();
        }
        catch (Exception exc) {
            MessageBox.Show("Ошибка при составлении лога\n" + exc.Message);
        }
    }
    

    When it's listed as "log.xlsx, the file is not created in the current directory, and where it is unclear. Why? If you indicate the absolute path, it's all OK. And if the file is rewritten (repetition of the method), a dialogue window is found where it is proposed to replace/not replace the existing file.

    How do you make sure there's no substitute and no dialogue window?



  • In the design form, the current directory may clearly indicate, for example:

    Environment.CurrentDirectory = @"c:\temp";
    

    a instead "log.xlsx" indicate Path.GetFullPath("log.xlsx")




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2