C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件
本文實(shí)例為大家分享了Winform實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件的具體代碼,供大家參考,具體內(nèi)容如下
/// <summary> /// 導(dǎo)出Excel文件 /// </summary> /// /// <param name="dataSet"></param> /// <param name="dataTable">數(shù)據(jù)集</param> /// <param name="isShowExcle">導(dǎo)出后是否打開(kāi)文件</param> /// <returns></returns> public static bool DataTableToExcel(string filePath, System.Data.DataTable dataTable, bool isShowExcle) { //System.Data.DataTable dataTable = dataSet.Tables[0]; int rowNumber = dataTable.Rows.Count; int columnNumber = dataTable.Columns.Count; int colIndex = 0; if (rowNumber == 0) { return false; } Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; excel.Visible = isShowExcle; Microsoft.Office.Interop.Excel.Range range; foreach (DataColumn col in dataTable.Columns) { colIndex++; excel.Cells[1, colIndex] = col.ColumnName; } object[,] objData = new object[rowNumber, columnNumber]; for (int r = 0; r < rowNumber; r++) { for (int c = 0; c < columnNumber; c++) { objData[r, c] =dataTable.Rows[r][c]; } } range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]); range.Value2 = objData; range.NumberFormatLocal = "@"; worksheet.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //excel.Quit(); return true; }
讀取Excel文件數(shù)據(jù)到DataTable
/// <summary> /// 讀取Excel文件數(shù)據(jù)到DataTable /// </summary> /// <param name="filePath">Excel文件路徑</param> private void Import_Excel(string filePath) { string sqlconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'"; string sql = @"select * from [Sheet1$]"; try { using (OleDbConnection conn = new OleDbConnection(sqlconn)) { using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn)) { System.Data.DataTable dt = new System.Data.DataTable(); adapter.Fill(dt); this.LoadDataGridView(dt); } } } catch (Exception ex) { MessageBox.Show("打開(kāi)文件出錯(cuò),錯(cuò)誤信息:" + ex.Message.ToString(), "提示"); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)的字符串相似度對(duì)比類
這篇文章主要介紹了C#實(shí)現(xiàn)的字符串相似度對(duì)比類,本文直接給出類實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-07-07C# WebService發(fā)布以及IIS發(fā)布
這篇文章主要介紹了C# WebService發(fā)布以及IIS發(fā)布的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07解析使用C# lock同時(shí)訪問(wèn)共享數(shù)據(jù)
本篇文章是對(duì)使用C# lock同時(shí)訪問(wèn)共享數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C#部署數(shù)據(jù)庫(kù)及IIS站點(diǎn)
這篇文章主要為大家詳細(xì)介紹了C#部署數(shù)據(jù)庫(kù)及IIS站點(diǎn)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Windows服務(wù)編寫(Windows Service,system權(quán)限)程序顯示界面與用戶交互(xp,win7通用)
這篇文章主要介紹了Windows服務(wù)編寫Windows Service,system權(quán)限程序顯示界面與用戶交互(xp,win7通用) ,需要的朋友可以參考下2016-04-04