C#導(dǎo)入導(dǎo)出Excel數(shù)據(jù)的兩種方法
本文為大家分享了C#導(dǎo)入導(dǎo)出Excel數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
注:對于實(shí)體類對象最好新建一個并且繼承原有實(shí)體類,這樣可以將類型進(jìn)行修改;
方法一:此種方法是用EPPLUS中的FileInfo流進(jìn)行讀取的(是不是流我還真不太了解,若有懂得請留言,非常感謝了)
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對Excel進(jìn)行的操作,所以只能操作
//2007以后的版本以后的(即擴(kuò)展名為.xlsx)
if (!filePath.Trim().EndsWith("xlsx"))
throw new Exception("請使用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> 打開文件 </summary>
bool Open();
//ExcelVersion Version { get; }
/// <summary> 文件路徑 </summary>
string FilePath { get; set; }
/// <summary> 文件是否已經(jīng)打開 </summary>
bool IfOpen { get; }
/// <summary> 文件包含工作表的數(shù)量 </summary>
int SheetCount { get; }
/// <summary> 當(dāng)前工作表序號 </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">行序號</param>
int GetCellCountInRow(int Row);
/// <summary> 獲取當(dāng)前工作表中某一單元格的值(按字符串返回) </summary>
/// <param name="Row">行序號</param>
/// <param name="Col">列序號</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("工作表序號超出范圍");
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ù)行號和列號獲取指定單元格的數(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)功能:
//用于程序是在本地,所以此時的路徑是本地電腦的絕對路勁;
//當(dāng)程序發(fā)布后此路徑應(yīng)該是服務(wù)器上的絕對路徑,所以在此之前還要有
//一項(xiàng)功能是將本地文件上傳到服務(wù)器上的指定位置,此時在獲取路徑即可
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表,然后在對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的第一個sheet)
/// </summary>
/// <param name="fullFielPath">文件的絕對路徑</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];//選定 指定頁
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));
//剩下的寫入datatable
else
dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
}
}
return dt;
}
}
}
之前我有一個程序用的是方法一進(jìn)行Excel導(dǎo)入的,速度不是很快,后來我又用了第二種方法但是速度更慢了,到底這兩種方法哪種快,請指導(dǎo),還是我用第二種方法的時候業(yè)務(wù)判斷有問題,不得而知,就請明白人指導(dǎo)我到底這兩種方法哪種比較好些。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#給PDF文件添加水印
欄 目:C#教程
本文標(biāo)題:C#導(dǎo)入導(dǎo)出Excel數(shù)據(jù)的兩種方法
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5821.html
您可能感興趣的文章
- 01-10C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解
- 01-10C#讀取Excel的三種方式以及比較分析
- 01-10C#定制Excel界面并實(shí)現(xiàn)與數(shù)據(jù)庫交互的方法
- 01-10C#創(chuàng)建數(shù)據(jù)庫及導(dǎo)入sql腳本的方法
- 01-10C#實(shí)例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置
- 01-10C#使用Aspose.Cells控件讀取Excel
- 01-10C#實(shí)現(xiàn)Excel動態(tài)生成PivotTable
- 01-10C#的Excel導(dǎo)入、導(dǎo)出
- 01-10C#使用oledb導(dǎo)出數(shù)據(jù)到excel的方法
- 01-10C#.net編程創(chuàng)建Access文件和Excel文件的方法詳解


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


