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

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

    • 分享

      C#輸出Excel表格

       昵稱QAb6ICvc 2013-01-31

      關(guān)于c#操作Excel表格的代碼,網(wǎng)上一搜一堆,之前在給測(cè)繪局寫插件的時(shí)候就寫過(guò)了。下面的代碼是讓小師弟Z寫的,寫的很不錯(cuò),我拿過(guò)來(lái)直接就可以用了。從中貌似我發(fā)現(xiàn)了我之前存在的一個(gè)問(wèn)題,就是相應(yīng)的資源在使用完之后沒(méi)有及時(shí)釋放掉,有時(shí)就會(huì)Excel表格報(bào)錯(cuò)。查了下,少了一句代碼System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp);

      下面的代碼是將DataTable中的數(shù)據(jù)輸出到Excel表格中:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      private void btnExportExcel_Click(object sender, EventArgs e)
      {
          SaveFileDialog saveDlg = new SaveFileDialog();
          saveDlg.DefaultExt = "xls";
          saveDlg.Title = "輸出斷面表格";
          saveDlg.Filter = "Excel文件(.xls)|*.xls";
          if(saveDlg.ShowDialog()==DialogResult.OK)
          {
              try
              {
                  string strExcelName = saveDlg.FileName;
                  Excel.Application xlapp = default(Excel.Application);
                  Excel.Workbook xlbook = default(Excel.Workbook);
                  Excel.Worksheet xlsheet = default(Excel.Worksheet);
                  xlapp = new Excel.Application();
                  xlapp.Visible = false;
                  xlapp.DisplayAlerts = false;
        
                  System.Object missing = Type.Missing;
                  xlbook = xlapp.Workbooks.Add(missing);
                  xlsheet = (Excel.Worksheet)xlbook.Worksheets[1];
        
                  for (int m = 0; m < dt.Columns.Count;m++ )
                  {
                      xlsheet.Cells[1, m + 1] = dt.Columns[m].ColumnName;
                  }
        
                  for (int i = 0; i < dt.Rows.Count;i++ )
                  {
                      for (int j = 0; j < dt.Columns.Count;j++ )
                      {
                          xlsheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
                      }
                  }
        
                  xlbook.SaveCopyAs(strExcelName);
                  xlapp.Workbooks.Close();
                  xlapp.Quit();
                  System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp);
                  xlsheet = null;
                  xlbook = null;
                  xlapp = null;
        
                  MessageBox.Show("Excel導(dǎo)出成功");
        
              }
              catch (Exception ex)
              {
                  MessageBox.Show("保存出錯(cuò),原因如下:"+ex.Message.ToString());
              }
          }
      }

        本站是提供個(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)論公約

        類似文章 更多