简体   繁体   English

无法使用C#写入excel?

[英]Can't write to excel using C#?

I am writing a very simple program. 我正在编写一个非常简单的程序。 It runs without any errors. 它运行没有任何错误。 But nothing is written into the Excel file. 但是没有任何内容写入Excel文件。 Am I wrong somewhere? 我在某个地方错了吗?

  using Excel = Microsoft.Office.Interop.Excel;

  // I already add this reference into my project

  public Write()
  {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(Directory.GetCurrentDirectory() + "\\KKK.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        // This is all I want to write to my Excel file

        xlWorkSheet.Cells[1, 1] = "input";

        xlWorkBook.SaveAs(Directory.GetCurrentDirectory() + "\\KQ" + ThoiGian(), Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlApp);
        releaseObject(xlWorkBook);
        releaseObject(xlWorkSheet);
}

尝试

xlWorkSheet.Cells[1, 1].value = "input";     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM