乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#讀寫Excel的幾種方法

       行走在理想邊緣 2020-04-13

      1 使用Office自帶的庫
      前提是本機(jī)須安裝office才能運(yùn)行,且不同的office版本之間可能會(huì)有兼容問題,從Nuget下載 Microsoft.Office.Interop.Excel
      在這里插入圖片描述
      讀寫代碼如下:

      using Microsoft.Office.Interop.Excel;
      using Excel = Microsoft.Office.Interop.Excel;
      
              private void btn_Office_Click(object sender, EventArgs e)
              {
                  string importExcelPath = "E:\\import.xlsx";
                  string exportExcelPath = "E:\\export.xlsx";
                  //創(chuàng)建
                  Excel.Application xlApp = new Excel.Application();
                  xlApp.DisplayAlerts = false;
                  xlApp.Visible = false;
                  xlApp.ScreenUpdating = false;
                  //打開Excel
                  Excel.Workbook xlsWorkBook = xlApp.Workbooks.Open(importExcelPath, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                  System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                  System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
      
                  //處理數(shù)據(jù)過程,更多操作方法自行百度
                  Excel.Worksheet sheet = xlsWorkBook.Worksheets[1];//工作薄從1開始,不是0
                  sheet.Cells[1, 1] = "test";
      
                  //另存
                  xlsWorkBook.SaveAs(exportExcelPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                  //關(guān)閉Excel進(jìn)程
                  ClosePro(xlApp, xlsWorkBook);
              }
              public void ClosePro(Excel.Application xlApp, Excel.Workbook xlsWorkBook)
              {
                  if (xlsWorkBook != null)
                      xlsWorkBook.Close(true, Type.Missing, Type.Missing);
                  xlApp.Quit();
                  // 安全回收進(jìn)程
                  System.GC.GetGeneration(xlApp);
                  IntPtr t = new IntPtr(xlApp.Hwnd);   //獲取句柄
                  int k = 0;
                  GetWindowThreadProcessId(t, out k);   //獲取進(jìn)程唯一標(biāo)志
                  System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                  p.Kill();     //關(guān)閉進(jìn)程
              }
      

      2. 使用NPOI
      地址:https://github.com/tonyqus/npoi
      在不安裝office的時(shí)候也是可以讀寫的,速度很快,從Nuget下載 NPOI
      在這里插入圖片描述
      讀寫代碼如下:

      using System.IO;
      using NPOI;
      using NPOI.SS.UserModel;
      
              private void btn_NPOI_Click(object sender, EventArgs e)
              {
                  string importExcelPath = "E:\\import.xlsx";
                  string exportExcelPath = "E:\\export.xlsx";
                  IWorkbook workbook = WorkbookFactory.Create(importExcelPath);
                  ISheet sheet = workbook.GetSheetAt(0);//獲取第一個(gè)工作薄
                  IRow row = (IRow)sheet.GetRow(0);//獲取第一行
      
                  //設(shè)置第一行第一列值,更多方法請(qǐng)參考源官方Demo
                  row.CreateCell(0).SetCellValue("test");//設(shè)置第一行第一列值
      
                  //導(dǎo)出excel
                  FileStream fs = new FileStream(exportExcelPath, FileMode.Create, FileAccess.ReadWrite);
                  workbook.Write(fs);
                  fs.Close();
              }
      

      3. 使用ClosedXml
      地址:https://github.com/ClosedXML/ClosedXML
      從Nuget下載 ClosedXml
      在這里插入圖片描述
      讀寫代碼如下:

      using ClosedXML;
      using ClosedXML.Excel;
              private void btn_ClosedXML_Click(object sender, EventArgs e)
              {
                  string importExcelPath = "E:\\import.xlsx";
                  string exportExcelPath = "E:\\export.xlsx";
                  var workbook = new XLWorkbook(importExcelPath);
      
                  IXLWorksheet sheet = workbook.Worksheet(1);//這個(gè)庫也是從1開始
                  //設(shè)置第一行第一列值,更多方法請(qǐng)參考官方Demo
                  sheet.Cell(1, 1).Value = "test";//該方法也是從1開始,非0
      
                  workbook.SaveAs(exportExcelPath);
              }
      

      4. 使用 spire.xls
      地址:https://www./Introduce/free-xls-component.html
      spire分免費(fèi)和收費(fèi),無特殊需求用免費(fèi)即可
      從Nuget下載 Free Spire.xls For .NET
      在這里插入圖片描述
      讀寫代碼如下:

      using Spire.Xls;
      
              private void btnSpire_Click(object sender, EventArgs e)
              {
                  string importExcelPath = "E:\\import.xlsx";
                  string exportExcelPath = "E:\\export.xlsx";
      
                  Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
                  workbook.LoadFromFile(importExcelPath);
                  //處理Excel數(shù)據(jù),更多請(qǐng)參考官方Demo
                  Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
                  sheet.Range[1,1].Text = "test";//該方法也是從1開始,非0
      
                  workbook.SaveToFile(exportExcelPath);
              }
      
      1. EPPLUS
        地址:https://github.com/pruiz/EPPlus/tree/master/EPPlus
        沒用過這個(gè),就不做介紹了

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多