zoukankan      html  css  js  c++  java
  • C# Note38: Export data into Excel

    • Microsoft.Office.Interop.Excel
    1. You have to have Excel installed.
    2. Add a reference to your project to the excel interop dll. To do this on the .NET tab select Microsoft.Office.Interop.Excel. There could be multiple assemblies with this name. Select the appropriate for your Visual Studio AND Excel version.
    3. Here is a code sample to create a new Workbook and fill a column with the items from your list
    //if you want to make excel visible           
        excapp.Visible = true;
    
        //create a blank workbook
        var workbook = excapp.Workbooks.Add(NsExcel.XlWBATemplate.xlWBATWorksheet);
    
        //or open one - this is no pleasant, but yue're probably interested in the first parameter
        string workbookPath = "C:	est.xls";
        var workbook = excapp.Workbooks.Open(workbookPath,
            0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
            true, false, 0, true, false, false);
    
        //Not done yet. You have to work on a specific sheet - note the cast
        //You may not have any sheets at all. Then you have to add one with NsExcel.Worksheet.Add()
        var sheet = (NsExcel.Worksheet)workbook.Sheets[1]; //indexing starts from 1
    
        //do something usefull: you select now an individual cell
        var range = sheet.get_Range("A1", "A1");
        range.Value2 = "test"; //Value2 is not a typo
    
        //now the list
        string cellName;
        int counter = 1;
        foreach (var item in list)
        {
            cellName = "A" + counter.ToString();
            var range = sheet.get_Range(cellName, cellName);
            range.Value2 = item.ToString();
            ++counter;
        }
    
        //you've probably got the point by now, so a detailed explanation about workbook.SaveAs and workbook.Close is not necessary
        //important: if you did not make excel visible terminating your application will terminate excel as well - I tested it
        //but if you did it - to be honest - I don't know how to close the main excel window - maybee somewhere around excapp.Windows or excapp.ActiveWindow
    }
    

     

    Using the CSV idea

    using System.IO;
    
    using(StreamWriter sw = File.CreateText("list.csv"))
    {
      for(int i = 0; i < l.Count; i++)
      {
        sw.WriteLine(l[i]);
      }
    }
    

      

    Using ClosedXML library( there is no need to install MS Excel

     

    https://github.com/closedxml/closedxml/wiki

    https://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-generic-listt-to-excel-in-C-Sharp-using-interop/

  • 相关阅读:
    HTTP协议
    UI- 不易记知识点汇总
    UI- 五种手势识别总结
    idea整合 springboot jsp mybatis
    xml和map互转工具类
    ajax请求案例
    java加密工具类,可设置对应的加解密key
    ajax请求正常,返回json格式,后台没问题,浏览器500
    通过工具SecureCRTPortable将项目部署到服务器上
    修改idea自动生成在C盘的文件路径,以免电脑越用越卡
  • 原文地址:https://www.cnblogs.com/carsonzhu/p/8990806.html
Copyright © 2011-2022 走看看