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

C#使用Datatable導(dǎo)出Excel

 更新時(shí)間:2018年10月04日 10:03:14   作者:薛定諤家的貓  
這篇文章主要為大家詳細(xì)介紹了C#使用Datatable導(dǎo)出Excel的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#使用Datatable導(dǎo)出Excel的具體代碼,供大家參考,具體內(nèi)容如下

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
namespace EasyFrame.Common
{
 /// <summary>
 ///title調(diào)用信息
 /// </summary>
 public class ChartTitle
 {
  /// <summary>
  /// 調(diào)用名
  /// </summary>
  public string CallName { get; set; }
  /// <summary>
  /// 標(biāo)頭信息
  /// </summary>
  public List<ChartName> SetNameList { get; set; }
 }
 public class ChartName
 {
  /// <summary>
  /// 一級(jí)標(biāo)頭
  /// </summary>
  public string Name { get; set; }
  /// <summary>
  /// 二級(jí)標(biāo)頭
  /// </summary>
  public List<string> ChartChildNameList { get; set; }

 }

 public class ExcelHelper
 {

  //
  /// <summary>
  /// Datatable導(dǎo)出Excel
  /// </summary>
  /// <param name="dt">數(shù)據(jù)</param>
  /// <param name="list">表頭</param>
  /// <param name="counts">總條數(shù)</param>
  /// <param name="filePath">保存地址</param>
  /// <returns></returns>
  public static MemoryStream WriteExcel(DataTable dt, List<ChartTitle> objlist, string filePath)
  {
   var list = objlist.FirstOrDefault(a => a.CallName == "ccc").SetNameList;
   if (!string.IsNullOrEmpty(filePath) && null != dt && dt.Rows.Count > 0)
   {
    XSSFWorkbook book = new XSSFWorkbook();
    NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(dt.TableName);
    ICell cell = null;
    ICellStyle style = book.CreateCellStyle();
    IFont font = book.CreateFont();//創(chuàng)建字體樣式 
    IFont fonts = book.CreateFont();//創(chuàng)建字體樣式 
    IFont fontss = book.CreateFont();//創(chuàng)建字體樣式 
    font.Color = NPOI.HSSF.Util.HSSFColor.Red.Index;//設(shè)置字體顏色
    ICellStyle styles = book.CreateCellStyle(); //紅色
    fonts.Color = NPOI.HSSF.Util.HSSFColor.Green.Index;
    ICellStyle styless = book.CreateCellStyle(); //綠色
    fontss.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;
    ICellStyle stylesss = book.CreateCellStyle(); //藍(lán)色
    styless.SetFont(fonts);
    styles.SetFont(font);
    stylesss.SetFont(fontss);
    style.Alignment = HorizontalAlignment.Center;
    NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(0);
    NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(1);
    var row = row1;
    int num = 0;//記錄列
    var d = true;//是否第一次加載到二級(jí)表單
    #region 創(chuàng)建表頭
    //兩行單列
    for (int i = 0; i < list.Count; i++)
    {
     if (!d)
     {
      row = row1;
      d = true;
     }
     if (list[i].ChartChildNameList == null)
     {
      cell = row.CreateCell(num);
      sheet.SetColumnWidth(num, 15 * 256);
      cell.SetCellValue(list[i].Name);
      cell.CellStyle = style;
      sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, num, num));
      num++;
      continue;
     }
     //兩行多列
     var count = list[i].ChartChildNameList.Count; //列數(shù)
     cell = row.CreateCell(num);
     cell.SetCellValue(list[i].Name);
     cell.CellStyle = style;
     sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, num, count + num - 1));
     if (d)
     {
      d = false;
      row = row2;
     }
     var t = 0;
     var rang = sheet.GetRow(1);
     for (int j = 0; j < count; j++)
     {
      sheet.SetColumnWidth(num + t, 5 * 256);
      cell = row.CreateCell(num + t);
      cell.SetCellValue(list[i].ChartChildNameList[j]);
      cell.CellStyle = style;
      t++;
     }
     num = num + count;
    }
    #endregion 創(chuàng)建表頭
    #region 寫入數(shù)據(jù)
    int customs = 0;//記錄列數(shù)
    int clouns = 0;//記錄行數(shù)



    filePath = string.Format(filePath, "XXX");
    #region 導(dǎo)出操作
    var data1 = JsonConvert.DeserializeObject<List<類名>>(dt.Rows[0][0].ToString());
    foreach (var item in data1)
    {
     NPOI.SS.UserModel.IRow rows = sheet.CreateRow(clouns + 2);
     customs = 0;
     rows.CreateCell(customs).SetCellValue(Convert.ToString(item.No)); customs++;
     rows.CreateCell(customs).SetCellValue(Convert.ToString(item.LotteryOpenNo)); customs++;
     for (int i = 0; i < item.Wan.Length; i++)
     {
      if (item.LotteryOpenNo.Split(',')[0] == item.Wan[i].ToString())
      {
       cell = rows.CreateCell(customs);
       cell.SetCellValue(item.Wan[i]);
       cell.CellStyle = styles;
       customs++;
      }
      else
      {
       rows.CreateCell(customs).SetCellValue(item.Wan[i]); customs++;
      }
     }
     for (int j = 0; j < item.Qian.Length; j++)
     {
      if (item.LotteryOpenNo.Split(',')[1] == item.Qian[j].ToString())
      {
       cell = rows.CreateCell(customs);
       cell.SetCellValue(item.Qian[j]);
       cell.CellStyle = styles;
       customs++;
      }
      else
      {
       rows.CreateCell(customs).SetCellValue(item.Qian[j]); customs++;
      }
     }
     for (int k = 0; k < item.Bai.Length; k++)
     {
      if (item.LotteryOpenNo.Split(',')[2] == item.Bai[k].ToString())
      {
       cell = rows.CreateCell(customs);
       cell.SetCellValue(item.Bai[k]);
       cell.CellStyle = styles;
       customs++;
      }
      else
      {
       rows.CreateCell(customs).SetCellValue(item.Bai[k]); customs++;
      }
     }
     for (int l = 0; l < item.Shi.Length; l++)
     {
      if (item.LotteryOpenNo.Split(',')[3] == (item.Shi[l].ToString()))
      {
       cell = rows.CreateCell(customs);
       cell.SetCellValue(item.Shi[l]);
       cell.CellStyle = styles;
       customs++;
      }
      else
      {
       rows.CreateCell(customs).SetCellValue(item.Shi[l]); customs++;
      }
     }
     for (int m = 0; m < item.Ge.Length; m++)
     {
      if (item.LotteryOpenNo.Split(',')[4] == (item.Ge[m].ToString()))
      {
       cell = rows.CreateCell(customs);
       cell.SetCellValue(item.Ge[m]);
       cell.CellStyle = styles;
       customs++;
      }
      else
      {
       rows.CreateCell(customs).SetCellValue(item.Ge[m]); customs++;
      }
     }
     for (int n = 0; n < item.Fen.Length; n++)
     {
      if (item.LotteryOpenNo.Contains(item.Fen[n].ToString()))
      {
       cell = rows.CreateCell(customs);
       cell.SetCellValue(item.Fen[n]);
       cell.CellStyle = styles;
       customs++;
      }
      else
      {
       rows.CreateCell(customs).SetCellValue(item.Fen[n]); customs++;
      }

     }
     clouns++;
    }

    #endregion


    #endregion 寫入數(shù)據(jù)
    // 寫入到客戶端 
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
     book.Write(ms);
     using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
     {
      byte[] data = ms.ToArray();
      fs.Write(data, 0, data.Length);
      fs.Flush();
     }
     book = null;
     return ms;
    }
   }
   return null;
  }



 }
}

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

相關(guān)文章

  • C#學(xué)習(xí)基礎(chǔ)概念二十五問 11-15

    C#學(xué)習(xí)基礎(chǔ)概念二十五問 11-15

    C#學(xué)習(xí)基礎(chǔ)概念二十五問 11-15...
    2007-04-04
  • Unity實(shí)現(xiàn)倒計(jì)時(shí)功能

    Unity實(shí)現(xiàn)倒計(jì)時(shí)功能

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C# 根據(jù)字符串生成二維碼的實(shí)例代碼

    C# 根據(jù)字符串生成二維碼的實(shí)例代碼

    這篇文章主要介紹了C# 根據(jù)字符串生成二維碼的實(shí)例,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng)

    C#實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • c#將list類型轉(zhuǎn)換成DataTable方法示例

    c#將list類型轉(zhuǎn)換成DataTable方法示例

    將List類型轉(zhuǎn)換成DataTable的通用方法,大家參考使用吧
    2013-12-12
  • C#?利用Autofac批量接口注入依賴的問題小結(jié)

    C#?利用Autofac批量接口注入依賴的問題小結(jié)

    這篇文章主要介紹了C#?利用Autofac批量接口注入依賴的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • C#數(shù)據(jù)適配器DataAdapter

    C#數(shù)據(jù)適配器DataAdapter

    這篇文章介紹了C#中的數(shù)據(jù)適配器DataAdapter,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#模擬瀏覽器實(shí)現(xiàn)自動(dòng)操作

    C#模擬瀏覽器實(shí)現(xiàn)自動(dòng)操作

    這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)模擬瀏覽器實(shí)現(xiàn)自動(dòng)操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • C#中LINQ to Objects查詢的實(shí)現(xiàn)

    C#中LINQ to Objects查詢的實(shí)現(xiàn)

    LINQ to Objects是LINQ技術(shù)在C#中的一種應(yīng)用,它專門用于對(duì)內(nèi)存中的對(duì)象集合進(jìn)行查詢和操作,本文就詳細(xì)的介紹C#中LINQ to Objects查詢的實(shí)現(xiàn),感興趣的可以了解一下
    2023-08-08
  • unity置灰處理的實(shí)現(xiàn)

    unity置灰處理的實(shí)現(xiàn)

    本文主要介紹了unity置灰處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07

最新評(píng)論

北碚区| 泰安市| 招远市| 成安县| 澳门| 台前县| 临邑县| 康马县| 兴隆县| 大荔县| 石棉县| 龙陵县| 原平市| 竹北市| 马山县| 浮山县| 堆龙德庆县| 闽侯县| 肥西县| 股票| 额济纳旗| 子长县| 肇州县| 民乐县| 二连浩特市| 伽师县| 新昌县| 榕江县| 嘉黎县| 临江市| 拜城县| 鄂托克旗| 济阳县| 改则县| 门源| 获嘉县| 清水河县| 鄢陵县| 龙山县| 雷波县| 盖州市|