C#導入導出數(shù)據(jù)到Excel的通用類源碼

下面內(nèi)容是關(guān)于C#導入導出數(shù)據(jù)到Excel的通用類的內(nèi)容。

public class ExcelIO

{

? ? private int _ReturnStatus;

? ? private string _ReturnMessage;

? ? public int ReturnStatus

? ? {

? ? ? ? get{return _ReturnStatus;}

? ? }

? ? public string ReturnMessage

? ? {

? ? ? ? get{return _ReturnMessage;}

? ? }

? ? public ExcelIO()

? ? {

? ? }

? ? public DataSet ImportExcel(string fileName)

? ? {

? ? ? ? Excel.Application xlApp=new Excel.ApplicationClass();? ? ? ? ?

? ? ? ? if(xlApp==null)

? ? ? ? {

? ? ? ? ? ? _ReturnStatus = -1;

? ? ? ? ? ? _ReturnMessage = "無法創(chuàng)建Excel對象,可能您的計算機未安裝Excel";

? ? ? ? ? ? return null;

? ? ? ? }? ? ?

? ? ? ? Excel.Workbook workbook;? ? ? ? ? ? ? ?

? ? ? ? try

? ? ? ? {

? ? ? ? ? ? workbook = xlApp.Workbooks.Open(fileName,0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);

? ? ? ? }

? ? ? ? catch

? ? ? ? {

? ? ? ? ? ? _ReturnStatus = -1;

? ? ? ? ? ? _ReturnMessage = "Excel文件處于打開狀態(tài),請保存關(guān)閉";

? ? ? ? ? ? return null;

? ? ? ? }? ? ?


? ? ? ? int n = workbook.Worksheets.Count;

? ? ? ? string[] SheetSet = new string[n];

? ? ? ? System.Collections.ArrayList al = new System.Collections.ArrayList();

? ? ? ? for(int i=1; i<=n; i++)

? ? ? ? {

? ? ? ? ? ? SheetSet[i-1] = ((Excel.Worksheet)workbook.Worksheets[i]).Name;

? ? ? ? }


? ? ? ? workbook.Close(null,null,null);? ? ? ?

? ? ? ? xlApp.Quit();

? ? ? ? if(workbook != null)

? ? ? ? {

? ? ? ? ? ? System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

? ? ? ? ? ? workbook = null;

? ? ? ? }

? ? ? ? if(xlApp != null)

? ? ? ? {

? ? ? ? ? ? System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

? ? ? ? ? ? xlApp = null;

? ? ? ? }?

? ? ? ? GC.Collect();


? ? ? ? DataSet ds = new DataSet();? ? ? ?

? ? ? ? string connStr = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+ fileName +";Extended Properties=Excel 8.0" ;

? ? ? ? using(OleDbConnection conn = new OleDbConnection (connStr))

? ? ? ? {

? ? ? ? ? ? conn.Open();

? ? ? ? ? ? OleDbDataAdapter da;

? ? ? ? ? ? for(int i=1; i<=n; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? da = new OleDbDataAdapter(sql,conn);

? ? ? ? ? ? ? ? da.Fill(ds,SheetSet[i-1]);?

? ? ? ? ? ? ? ? da.Dispose();

? ? ? ? ? ? }? ? ? ? ? ? ?

? ? ? ? ? ? conn.Close();

? ? ? ? ? ? conn.Dispose();

? ? ? ? }? ? ? ? ? ? ?

? ? ? ? return ds;

? ? }

? ? public bool ExportExcel(string reportName,DataTable dt,string saveFileName)

? ? {

? ? ? ? if(dt==null)

? ? ? ? {

? ? ? ? ? ? _ReturnStatus = -1;

? ? ? ? ? ? _ReturnMessage = "數(shù)據(jù)集為空!";

? ? ? ? ? ? return false;? ? ? ? ?

? ? ? ? }

? ? ? ? bool fileSaved=false;

? ? ? ? Excel.Application xlApp=new Excel.ApplicationClass();?

? ? ? ? if(xlApp==null)

? ? ? ? {

? ? ? ? ? ? _ReturnStatus = -1;

? ? ? ? ? ? _ReturnMessage = "無法創(chuàng)建Excel對象,可能您的計算機未安裝Excel";

? ? ? ? ? ? return false;

? ? ? ? }

? ? ? ? Excel.Workbooks workbooks=xlApp.Workbooks;

? ? ? ? Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

? ? ? ? worksheet.Cells.Font.Size = 10;

? ? ? ? Excel.Range range;

? ? ? ? long totalCount=dt.Rows.Count;

? ? ? ? long rowRead=0;

? ? ? ? float percent=0;

? ? ? ? worksheet.Cells[1,1]=reportName;

? ? ? ? ((Excel.Range)worksheet.Cells[1,1]).Font.Size = 12;

? ? ? ? ((Excel.Range)worksheet.Cells[1,1]).Font.Bold = true;

? ? ? ? for(int i=0;i<dt.Columns.Count;i++)

? ? ? ? {

? ? ? ? ? ? worksheet.Cells[2,i+1]=dt.Columns[i].ColumnName;

? ? ? ? ? ? range=(Excel.Range)worksheet.Cells[2,i+1];

? ? ? ? ? ? range.Interior.ColorIndex = 15;

? ? ? ? ? ? range.Font.Bold = true;

? ? ? ? }

? ? ? ? for(int r=0;r<dt.Rows.Count;r++)

? ? ? ? {

? ? ? ? ? ? for(int i=0;i<dt.Columns.Count;i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? worksheet.Cells[r+3,i+1]=dt.Rows[r][i].ToString();

? ? ? ? ? ? }

? ? ? ? ? ? rowRead++;

? ? ? ? }


? ? ? ? range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[dt.Rows.Count+2,dt.Columns.Count]);

? ? ? ? range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);

? ? ? ? if( dt.Rows.Count > 0)

? ? ? ? {

? ? ? ? ? ? range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

? ? ? ? ? ? range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;

? ? ? ? ? ? range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;

? ? ? ? }

? ? ? ? if(dt.Columns.Count>1)

? ? ? ? {

? ? ? ? ? ? range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;

? ? ? ? ? ? range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;

? ? ? ? ? ? range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;

? ? ? ? }

? ? ? ? if(saveFileName!="")

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? workbook.Saved =true;

? ? ? ? ? ? ? ? workbook.SaveCopyAs(saveFileName);

? ? ? ? ? ? ? ? fileSaved=true;

? ? ? ? ? ? }

? ? ? ? ? ? catch(Exception ex)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? fileSaved=false;

? ? ? ? ? ? ? ? _ReturnStatus = -1;

? ? ? ? ? ? ? ? _ReturnMessage = "導出文件時出錯,文件可能正被打開!n"+ex.Message;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? fileSaved=false;

? ? ? ? }? ? ? ? ?


? ? ? ? if(range != null)

? ? ? ? {

? ? ? ? ? ? System.Runtime.InteropServices.Marshal.ReleaseComObject(range);

? ? ? ? ? ? range = null;

? ? ? ? }

? ? ? ? if(worksheet != null)

? ? ? ? {

? ? ? ? ? ? System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);

? ? ? ? ? ? worksheet = null;

? ? ? ? }

? ? ? ? if(workbook != null)

? ? ? ? {

? ? ? ? ? ? System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

? ? ? ? ? ? workbook = null;

? ? ? ? }

? ? ? ? if(workbooks != null)

? ? ? ? {

? ? ? ? ? ? System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);

? ? ? ? ? ? workbooks = null;

? ? ? ? }? ? ? ? ? ? ?

? ? ? ? xlApp.Application.Workbooks.Close();

? ? ? ? xlApp.Quit();

? ? ? ? if(xlApp != null)

? ? ? ? {

? ? ? ? ? ? System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

? ? ? ? ? ? xlApp = null;

? ? ? ? }

? ? ? ? GC.Collect();

? ? ? ? return fileSaved;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容