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

C#導(dǎo)入導(dǎo)出Excel數(shù)據(jù)的兩種方法

 更新時(shí)間:2017年03月21日 17:26:13   作者:路博林  
這篇文章主要為大家詳細(xì)介紹了C#導(dǎo)入導(dǎo)出Excel數(shù)據(jù)的兩種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了C#導(dǎo)入導(dǎo)出Excel數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下

注:對(duì)于實(shí)體類(lèi)對(duì)象最好新建一個(gè)并且繼承原有實(shí)體類(lèi),這樣可以將類(lèi)型進(jìn)行修改;

方法一:此種方法是用EPPLUS中的FileInfo流進(jìn)行讀取的(是不是流我還真不太了解,若有懂得請(qǐng)留言,非常感謝了)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Extensions;

namespace HYZT.Ltxy.International.Ctrip.Exporting
{
 public class ExcelLib
 {
  public ICtripPolicyExcelImport GetExcel(string filePath)
  {
   if (filePath.Trim() .IsNullOrEmpty())
    throw new Exception("文件名不能為空");
 //因?yàn)檫@兒用得是EPPLUS對(duì)Excel進(jìn)行的操作,所以只能操作
 //2007以后的版本以后的(即擴(kuò)展名為.xlsx)
   if (!filePath.Trim().EndsWith("xlsx"))
    throw new Exception("請(qǐng)使用office Excel 2007版本或2010版本");

   else if (filePath.Trim().EndsWith("xlsx"))
   {
    ICtripPolicyExcelImport res = new CtripPolicyExcelImport(filePath.Trim());
    return res;
   }
   else return null;
  }
 }
}

方法接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HYZT.Ltxy.International.Ctrip.Exporting
{
 public interface ICtripPolicyExcelImport
 {
  /// <summary> 打開(kāi)文件 </summary> 
  bool Open(); 
  //ExcelVersion Version { get; }
  /// <summary> 文件路徑 </summary> 
  string FilePath { get; set; }
  /// <summary> 文件是否已經(jīng)打開(kāi) </summary> 
  bool IfOpen { get; }
  /// <summary> 文件包含工作表的數(shù)量 </summary> 
  int SheetCount { get; }
  /// <summary> 當(dāng)前工作表序號(hào) </summary> 
  int CurrentSheetIndex { get; set; }
  /// <summary> 獲取當(dāng)前工作表中行數(shù) </summary> 
  int GetRowCount();
  /// <summary> 獲取當(dāng)前工作表中列數(shù) </summary> 
  int GetColumnCount();
  /// <summary> 獲取當(dāng)前工作表中某一行中單元格的數(shù)量 </summary> 
  /// <param name="Row">行序號(hào)</param> 
  int GetCellCountInRow(int Row);
  /// <summary> 獲取當(dāng)前工作表中某一單元格的值(按字符串返回) </summary> 
  /// <param name="Row">行序號(hào)</param> 
  /// <param name="Col">列序號(hào)</param> 
  string GetCellValue(int Row, int Col);
  /// <summary> 關(guān)閉文件 </summary> 
  void Close(); 
 }
}

方法實(shí)現(xiàn):

using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HYZT.Ltxy.International.Ctrip.Exporting
{
 public class CtripPolicyExcelImport:ICtripPolicyExcelImport
 {

  public CtripPolicyExcelImport() 
  { } 
 
  public CtripPolicyExcelImport(string path) 
  { filePath = path; }


  private string filePath = "";
  private ExcelWorkbook book = null;
  private int sheetCount = 0;
  private bool ifOpen = false;
  private int currentSheetIndex = 0;
  private ExcelWorksheet currentSheet = null;
  private ExcelPackage ep = null; 
 
   public bool Open() 
  { 
   try 
   { 
    ep = new ExcelPackage(new FileInfo(filePath)); 
     
    if (ep == null) return false; 
    book =ep.Workbook; 
    sheetCount = book.Worksheets.Count; 
    currentSheetIndex = 0; 
    currentSheet = book.Worksheets[1]; 
    ifOpen = true; 
   } 
   catch (Exception ex) 
   { 
    throw new Exception(ex.Message); 
   } 
   return true; 
  } 
 
  public void Close() 
  { 
   if (!ifOpen || ep == null) return; 
   ep.Dispose(); 
  } 
 
  //public ExcelVersion Version 
  //{ get { return ExcelVersion.Excel07; } } 
 
  public string FilePath 
  { 
   get { return filePath; } 
   set { filePath = value; } 
  } 
 
  public bool IfOpen 
  { get { return ifOpen; } } 
 
  public int SheetCount 
  { get { return sheetCount; } } 
 
  public int CurrentSheetIndex 
  { 
   get { return currentSheetIndex; } 
   set 
   { 
    if (value != currentSheetIndex) 
    { 
     if (value >= sheetCount) 
      throw new Exception("工作表序號(hào)超出范圍"); 
     currentSheetIndex = value; 
     currentSheet =book.Worksheets[currentSheetIndex+1]; 
    } 
   } 
  } 
 
  public int GetRowCount() 
  { 
   if (currentSheet == null) return 0; 
   return currentSheet.Dimension.End.Row; 
  } 
 
  public int GetColumnCount() 
  { 
   if (currentSheet == null) return 0; 
   return currentSheet.Dimension.End.Column; 
  } 
 
  public int GetCellCountInRow(int Row) 
  { 
   if (currentSheet == null) return 0; 
   if (Row >= currentSheet.Dimension.End.Row) return 0; 
   return currentSheet.Dimension.End.Column; 
  } 
 //根據(jù)行號(hào)和列號(hào)獲取指定單元格的數(shù)據(jù)
  public string GetCellValue(int Row, int Col) 
  { 
   if (currentSheet == null) return ""; 
   if (Row >= currentSheet.Dimension.End.Row || Col >= currentSheet.Dimension.End.Column) return ""; 
   object tmpO =currentSheet.GetValue(Row+1, Col+1); 
   if (tmpO == null) return ""; 
   return tmpO.ToString(); 
  }   
 }   
}

方法調(diào)用實(shí)現(xiàn)功能:

//用于程序是在本地,所以此時(shí)的路徑是本地電腦的絕對(duì)路勁;
//當(dāng)程序發(fā)布后此路徑應(yīng)該是服務(wù)器上的絕對(duì)路徑,所以在此之前還要有
//一項(xiàng)功能是將本地文件上傳到服務(wù)器上的指定位置,此時(shí)在獲取路徑即可
 public string GetExcelToCtripPolicy(string filePath)
  {
   ExcelLib lib = new ExcelLib();
   if (filePath == null)
    return new ReturnResult<bool>(false, "未找到相應(yīng)文件");
   string str= tmp.GetCellValue(i, j); 
   return str;
  }
 

方法二:將Excel表格轉(zhuǎn)化成DataTable表,然后在對(duì)DataTable進(jìn)行業(yè)務(wù)操作

using Abp.Application.Services;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HYZT.Ltxy.International.Ctrip.GetExcelToDataTable
{
 public class EPPlusHelperAppService:ApplicationService,IEPPlusHelperAppService
 {
  private static string GetString(object obj)
  {
   try
   {
    return obj.ToString();
   }
   catch (Exception ex)
   {
    return "";
   }
  }

  /// <summary>
  ///將指定的Excel的文件轉(zhuǎn)換成DataTable (Excel的第一個(gè)sheet)
  /// </summary>
  /// <param name="fullFielPath">文件的絕對(duì)路徑</param>
  /// <returns></returns>
  public DataTable WorksheetToTable(string filePath)
  {
   try
   {
    FileInfo existingFile = new FileInfo(filePath);

    ExcelPackage package = new ExcelPackage(existingFile);
    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];//選定 指定頁(yè)

    return WorksheetToTable(worksheet);
   }
   catch (Exception)
   {
    throw;
   }
  }

  /// <summary>
  /// 將worksheet轉(zhuǎn)成datatable
  /// </summary>
  /// <param name="worksheet">待處理的worksheet</param>
  /// <returns>返回處理后的datatable</returns>
  public static DataTable WorksheetToTable(ExcelWorksheet worksheet)
  {
   //獲取worksheet的行數(shù)
   int rows = worksheet.Dimension.End.Row;
   //獲取worksheet的列數(shù)
   int cols = worksheet.Dimension.End.Column;

   DataTable dt = new DataTable(worksheet.Name);
   DataRow dr = null;
   for (int i = 1; i <= rows; i++)
   {
    if (i > 1)
     dr = dt.Rows.Add();

    for (int j = 1; j <= cols; j++)
    {
     //默認(rèn)將第一行設(shè)置為datatable的標(biāo)題
     if (i == 1)
      dt.Columns.Add(GetString(worksheet.Cells[i, j].Value));
     //剩下的寫(xiě)入datatable
     else
      dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
    }
   }
   return dt;
  }
 }
}

之前我有一個(gè)程序用的是方法一進(jìn)行Excel導(dǎo)入的,速度不是很快,后來(lái)我又用了第二種方法但是速度更慢了,到底這兩種方法哪種快,請(qǐng)指導(dǎo),還是我用第二種方法的時(shí)候業(yè)務(wù)判斷有問(wèn)題,不得而知,就請(qǐng)明白人指導(dǎo)我到底這兩種方法哪種比較好些。

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

相關(guān)文章

  • WPF使用DrawingContext實(shí)現(xiàn)簡(jiǎn)單繪圖

    WPF使用DrawingContext實(shí)現(xiàn)簡(jiǎn)單繪圖

    這篇文章主要為大家詳細(xì)介紹了WPF如何使用DrawingContext實(shí)現(xiàn)簡(jiǎn)單繪圖,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解下
    2024-02-02
  • C#基于ScottPlot實(shí)現(xiàn)可視化的示例代碼

    C#基于ScottPlot實(shí)現(xiàn)可視化的示例代碼

    這篇文章主要為大家詳細(xì)介紹了C#如何基于ScottPlot實(shí)現(xiàn)可視化效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • C#路徑,文件,目錄及IO常見(jiàn)操作匯總

    C#路徑,文件,目錄及IO常見(jiàn)操作匯總

    這篇文章主要介紹了C#路徑,文件,目錄及IO常見(jiàn)操作,較為詳細(xì)的分析并匯總了C#關(guān)于路徑,文件,目錄及IO常見(jiàn)操作,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • c#調(diào)整圖片分辨率的實(shí)現(xiàn)示例

    c#調(diào)整圖片分辨率的實(shí)現(xiàn)示例

    本文主要介紹了c#調(diào)整圖片分辨率的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-10-10
  • C#怎樣實(shí)現(xiàn)文件下載斷點(diǎn)續(xù)傳

    C#怎樣實(shí)現(xiàn)文件下載斷點(diǎn)續(xù)傳

    這篇文章主要介紹了C#怎樣實(shí)現(xiàn)文件下載斷點(diǎn)續(xù)傳,對(duì)斷點(diǎn)續(xù)傳感興趣的同學(xué),可以參考下
    2021-04-04
  • C#?Winform實(shí)現(xiàn)進(jìn)度條顯示

    C#?Winform實(shí)現(xiàn)進(jìn)度條顯示

    這篇文章主要為大家詳細(xì)介紹了C#?Winform實(shí)現(xiàn)進(jìn)度條顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • c#簡(jiǎn)單工廠、工廠方法與抽象工廠的區(qū)別分析

    c#簡(jiǎn)單工廠、工廠方法與抽象工廠的區(qū)別分析

    看了網(wǎng)絡(luò)上很多關(guān)于設(shè)計(jì)模式的方法,有的模式看起來(lái)相似,但本質(zhì)還是區(qū)別很大的.像簡(jiǎn)單工廠,工廠方法和抽象工廠就有很明顯的區(qū)別.
    2013-03-03
  • C#使用SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表

    C#使用SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表

    這篇文章主要介紹了C#使用SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表的方法,較為詳細(xì)的講述了SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2014-10-10
  • C#使用Unity實(shí)現(xiàn)剪刀石頭布游戲

    C#使用Unity實(shí)現(xiàn)剪刀石頭布游戲

    這篇文章主要為大家詳細(xì)介紹了C#語(yǔ)言使用Unity實(shí)現(xiàn)剪刀石頭布游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C#?Winform實(shí)現(xiàn)復(fù)制文件顯示進(jìn)度

    C#?Winform實(shí)現(xiàn)復(fù)制文件顯示進(jìn)度

    這篇文章主要介紹了C#?Winform實(shí)現(xiàn)復(fù)制文件顯示進(jìn)度,用進(jìn)度條來(lái)顯示復(fù)制情況,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07

最新評(píng)論

永康市| 巫溪县| 鸡东县| 绥芬河市| 山西省| 都匀市| 龙江县| 上饶市| 崇文区| 绥滨县| 延川县| 和田县| 怀仁县| 四川省| 闸北区| 峨眉山市| 叙永县| 鞍山市| 错那县| 遂川县| 和顺县| 惠来县| 永嘉县| 疏附县| 临邑县| 神池县| 龙海市| 竹山县| 施甸县| 游戏| 江安县| 朔州市| 林甸县| 阜宁县| 天长市| 巨野县| 金华市| 黎川县| 醴陵市| 蓬溪县| 子长县|