這篇文章給大家分享的是有關(guān)C#如何操作Excel的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)是一家專業(yè)提供太康企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、成都網(wǎng)站建設(shè)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為太康眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。具體代碼:
//引用Microsoft.Office.Interop.Excel.dll文件 //添加using using Microsoft.Office.Interop.Excel; using Excel=Microsoft.Office.Interop.Excel; //設(shè)置程序運(yùn)行語(yǔ)言 System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); //創(chuàng)建Application Excel.Application xlApp = new Excel.Application(); //設(shè)置是否顯示警告窗體 excelApp.DisplayAlerts = false; //設(shè)置是否顯示Excel excelApp.Visible = false; //禁止刷新屏幕 excelApp.ScreenUpdating = false; //根據(jù)路徑path打開 Excel.Workbook xlsWorkBook = excelApp.Workbooks.Open(path, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //獲取Worksheet對(duì)象 Excel.Worksheet xlsWorkSheet = (Worksheet)xlsWorkBook.Worksheets["sales plan"]; ***獲取最后一行、一列的兩種方法*** //獲取已用的范圍數(shù)據(jù) int rowsCount = xlsWorkSheet.UsedRange.Rows.Count; int colsCount = xlsWorkSheet.UsedRange.Columns.Count; int rowsCount = xlsWorkSheet.get_Range("A65536", "A65536").get_End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row; int colsCount = xlsWorkSheet.get_Range("ZZ1", "ZZ1").get_End(Microsoft.Office.Interop.Excel.XlDirection.xlToLeft).Column; ***將Excel數(shù)據(jù)存入二維數(shù)組*** //rowsCount:大行 colsCount:大列 Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Cells[1, 1]; Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Cells[rowsCount, colsCount]; Range rng = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.get_Range(c1, c2); object[,] exceldata = (object[,])rng.get_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault); //在第一列的左邊插入一列 Excel.Range xlsColumns = (Excel.Range)xlsWorkSheet.Columns[1, System.Type.Missing]; xlsColumns.Insert(XlInsertShiftDirection.xlShiftToRight, Type.Missing); //xlsSheetTemplateMajor_Meisai.Cells.get_Range(xlsSheetTemplateMajor_Meisai.Cells[1, 1], xlsSheetTemplateMajor_Meisai.Cells[65535, 1]).Insert(Type.Missing, Type.Missing); Excel.Range rng; rng = worksheet.get_Range("A:A", "A:A"); rng.Insert(Excel.XlDirection.xlToRight, Excel.XlInsertFormatOrigin.xlFormatFromLeftOrAbove); Excel.Range rng = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Columns[12, Type.Missing]; rng.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); //刪除行 Range deleteRng = (Range)xlsWorkSheetSapExcel.Rows[2, System.Type.Missing]; deleteRng.Delete(Excel.XlDeleteShiftDirection.xlShiftUp); //刪除一列數(shù)據(jù) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 11]).Select(); ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 11]).EntireColumn.Delete(0); ((Excel.Range)xlsSheetShareMajor_Meisai.Cells[1, 3]).EntireColumn.Delete (0); //設(shè)置背景色為紅色 xlsWorkSheet.get_Range("A1", "A1").Interior.ColorIndex = 3; //設(shè)置Format屬性 屬性值可以通過在vba中錄宏得到 Microsoft.Office.Interop.Excel.Range range1 = xlsWorkSheetAdd.get_Range("J1", "J65535"); range1.NumberFormat = "@";//文本格式 range1 = xlsWorkSheetAdd.get_Range("L1", "L65535"); range1.NumberFormat = "0.00";//保留兩位小數(shù) Excel.Range rng = xlsSheetShareMajor_Meisai.Columns["I", System.Type.Missing] as Excel.Range; rng.NumberFormatLocal =@"yyyy/m/d";//設(shè)置日期格式 //替換 Range Drng = xlsWorkSheetTemplate.get_Range("D1", "D65535"); Drng.Replace(" ", "", XlLookAt.xlPart, XlSearchOrder.xlByColumns, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); Drng.TextToColumns(Drng, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierSingleQuote, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //分列處理 設(shè)置為文本 Range TextToColumnRng = xlsWorkSheet.get_Range("E1", "E65535"); xlsWorkSheet.get_Range("E1", "E65535").TextToColumns(TextToColumnRng, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierSingleQuote, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //設(shè)置公式 rng = xlMergeFileWorkSheet.get_Range("D2", "D" + rowcount);//設(shè)置列范圍 rng.Formula = @"=IF(RC[-3]=""H"",""Survivor"",""Donor"")";//設(shè)置公式 @的問題 //rng.NumberFormat = "$0.00";//設(shè)置格式 copyRng = xlsSheetTemplateMajor_Meisai.get_Range("N3", "N" + lastRowTemplate); copyRng.Formula = "=VLOOKUP(RC[-12],AR殘!C[-13]:C[-11],2,0)"; //通過行、列的索引獲取值 string f = Convert.ToString(xlsSheetShareMajor_Meisai.get_Range(xlsSheetShareMajor_Meisai.Cells[2, 1], xlsSheetShareMajor_Meisai.Cells[2,1]).Value2); //篩選 //確定篩選范圍 D1_rng = D1_TemSheet.Cells.get_Range(D1_TemSheet.Cells[1, 1], D1_TemSheet.Cells[1, 50]); //執(zhí)行篩選動(dòng)作 rng.AutoFilter(5, "S", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true); rng.AutoFilter(6, "H", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlOr, "F", true); D1_rng.AutoFilter(D1_Column + 2, "#N/A", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues,"#N/A",false); //復(fù)制粘貼 D2_rng.Copy(Type.Missing); D2_TemSheet.Cells.get_Range(D2_TemSheet.Cells[2, (D2_Column + 1)], D2_TemSheet.Cells[2, (D2_Column + 1)]).PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false); //Find查找 rng = mySheet.get_Range("A1", "IV10").Find(arrLabel[j], Type.Missing, Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing); //文字占滿單元格 range.EntireColumn.AutoFit(); //另存 xlsWorkBook.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); ***關(guān)閉對(duì)象*** System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkBook); excelApp.Quit(); Kill(excelApp);//調(diào)用方法關(guān)閉進(jìn)程 GC.Collect(); /// <summary> /// 關(guān)閉Excel進(jìn)程 /// </summary> public class KeyMyExcelProcess { [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); public static void Kill(Microsoft.Office.Interop.Excel.Application excel) { try { IntPtr t = new IntPtr(excel.Hwnd); //得到這個(gè)句柄,具體作用是得到這塊內(nèi)存入口 int k = 0; GetWindowThreadProcessId(t, out k); //得到本進(jìn)程唯一標(biāo)志k System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到對(duì)進(jìn)程k的引用 p.Kill(); //關(guān)閉進(jìn)程k } catch (System.Exception ex) { throw ex; } } } //關(guān)閉打開的Excel方法 public void CloseExcel(Microsoft.Office.Interop.Excel.Application ExcelApplication, Microsoft.Office.Interop.Excel.Workbook ExcelWorkbook) { ExcelWorkbook.Close(false, Type.Missing, Type.Missing); ExcelWorkbook = null; ExcelApplication.Quit(); GC.Collect(); KeyMyExcelProcess.Kill(ExcelApplication); }
感謝各位的閱讀!關(guān)于“C#如何操作Excel”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
文章題目:C#如何操作Excel-創(chuàng)新互聯(lián)
URL地址:http://m.rwnh.cn/article22/cciejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、App設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容