最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#WinFrom導(dǎo)出Excel過程解析

 更新時(shí)間:2019年11月01日 17:02:57   作者:最愛吃湯圓27  
這篇文章主要介紹了C#WinFrom導(dǎo)出Excel過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了C#WinFrom導(dǎo)出Excel過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

采用的是以DataGridView的形式導(dǎo)出,使用NPOI.dll

1.由于使用的是DataGridView,所以類需要?jiǎng)?chuàng)建在From的Project下,DLL導(dǎo)入NPOI

2.代碼如下

ExportExcel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.SS.UserModel;    //NPOI
using NPOI.HSSF.Util;      //NPOI
using NPOI.HSSF.UserModel;   //NPOI
using NPOI.XSSF.UserModel;   //NPOI
using System.IO;
namespace ESMT
{
  public class ExportExcel
  {
    /// <summary>
    /// 
    /// </summary>
    /// <param name="grdview">數(shù)據(jù)表</param>
    /// <param name="sheetName">工作簿名字</param>
    /// <param name="FilePath">文件路徑</param>
    /// <param name="columnTitle">列頭</param>
    public void ExportToExcel(DataGridView grdview, string sheetName, string FilePath, string[] columnTitle)
    {

      //不允許dataGridView顯示添加行,負(fù)責(zé)導(dǎo)出時(shí)會(huì)報(bào)最后一行未實(shí)例化錯(cuò)誤
      grdview.AllowUserToAddRows = false;
      HSSFWorkbook workbook = new HSSFWorkbook();
      ISheet sheet = workbook.CreateSheet(sheetName);//創(chuàng)建工作簿
      //設(shè)置表頭
      IRow headerRow = sheet.CreateRow(0);//創(chuàng)建第一行
      headerRow.HeightInPoints = 40;
      headerRow.CreateCell(0).SetCellValue("出庫表單");//單元格賦值
      ICellStyle headStyle = workbook.CreateCellStyle();
      headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//格式居中
      IFont font = workbook.CreateFont();
      font.Boldweight = 500;
      font.FontHeightInPoints = 20;
      headStyle.SetFont(font);
      headerRow.GetCell(0).CellStyle = headStyle;
      sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, grdview.ColumnCount - 2));//單元格合并 最后個(gè)參數(shù)是合并個(gè)數(shù)

      IRow headerRow2 = sheet.CreateRow(1);//創(chuàng)建第二行列頭
      ICellStyle headStyle2 = workbook.CreateCellStyle();
      headStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
      IFont font2 = workbook.CreateFont();
      font2.FontHeightInPoints = 10;
      font2.Boldweight = 700;
      headStyle2.SetFont(font2);
      for (int l = 0; l < grdview.ColumnCount - 1; l++) //列頭填值
      {
        headerRow2.CreateCell(l).SetCellValue(columnTitle[l]);
        headerRow2.GetCell(l).CellStyle = headStyle2;
      }

      //設(shè)置列寬
      for (int l = 0; l < grdview.Columns.Count; l++)
      {
        sheet.DefaultColumnWidth = 15;
      }

      //填寫內(nèi)容
      for (int i = 0; i < grdview.Rows.Count; i++)
      {
        IRow row = sheet.CreateRow(i + 2);
        for (int j = 1; j < grdview.Columns.Count; j++)
        {
          row.CreateCell(j - 1, CellType.String).SetCellValue(grdview.Rows[i].Cells[j].Value.ToString());//j-1表示哪個(gè)單元格
        }
      }

      using (FileStream stream = File.OpenWrite(FilePath))//創(chuàng)建Excel并寫入數(shù)據(jù)
      {
        workbook.Write(stream);
        stream.Close();
      }
      GC.Collect();
    }
  }
}

PS:openwtrie 打開或者創(chuàng)建新的文件寫入

3.From窗口點(diǎn)擊導(dǎo)出按鈕

導(dǎo)出按鈕

string[] columnTitle = { "序號(hào)", "倉位", "Facility", "供應(yīng)商料號(hào)", "料號(hào)", "料卷ID", "料卷數(shù)量", "儲(chǔ)位號(hào)", "Date Code/Lot", "生產(chǎn)日期", "供應(yīng)商編碼", "入倉時(shí)間" };
      string localFilePath = "";// fileNameExt, newFileName, FilePath; 
      SaveFileDialog sfd = new SaveFileDialog();//保存文件窗口
      //設(shè)置文件類型 
      sfd.Filter = "Excel(97-2003)|*.xls";//保存類型為EXCEL
      //保存對話框是否記憶上次打開的目錄 
      sfd.RestoreDirectory = true;

      //點(diǎn)了保存按鈕進(jìn)入 
      if (sfd.ShowDialog() == DialogResult.OK)
      {
        localFilePath = sfd.FileName.ToString(); //獲得文件路徑 
        ex.ExportToExcel(grdData, "出庫表單", localFilePath, columnTitle);
      }

通過以上三步,完成點(diǎn)擊導(dǎo)出按鈕,后選擇保存位置并命名,調(diào)用EportExcel方法完成導(dǎo)出Excel。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

蓬安县| 徐闻县| 芜湖市| 抚宁县| 中牟县| 德兴市| 贵定县| 安平县| 东源县| 汽车| 江陵县| 民乐县| 衡南县| 光山县| 庆元县| 彰化县| 越西县| 南通市| 普兰县| 云和县| 诸暨市| 囊谦县| 团风县| 高台县| 千阳县| 松原市| 新泰市| 长垣县| 遵化市| 灌云县| 东台市| 东宁县| 阳朔县| 安福县| 巴楚县| 城口县| 田林县| 全州县| 城市| 阿克陶县| 荃湾区|