国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
在C#中調(diào)用Excel

1. 調(diào)用Excel的COM組件。
    在項(xiàng)目中打開Add Reference對(duì)話框,選擇COM欄,之后在COM列表中找到"Microsoft Excel 11.0 Object Library"(Office 2003),然后將其加入到項(xiàng)目的References中即可。Visual C#.NET會(huì)自動(dòng)產(chǎn)生相應(yīng)的.NET組件文件,以后即可正常使用。

 

 

2. 打開Excel表格
    Excel.Application excel = new Excel.Application(); //引用Excel對(duì)象
    Excel.Workbook book = excel.Application.Workbooks.Add(Missing.Value); //引用Excel工作簿
    excel.Visible = bVisible; //使Excel可視

有時(shí)調(diào)用excel.Application.Workbooks.Add(Missing.Value)會(huì)遇到如下錯(cuò)誤:
    Exception:
        Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
這是Excel自身的一個(gè)bug,當(dāng)本地系統(tǒng)環(huán)境被設(shè)置成非英文的,而Excel是英文的時(shí)候,就會(huì)出現(xiàn),需要臨時(shí)設(shè)定英文環(huán)境,代碼如下:
    System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

 

 

3. 往Excel表格中插入數(shù)據(jù)
    Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets["Sheet1"]; // 選中當(dāng)前新建Sheet(一般為Sheet1)
有兩種插入方法
a. 逐格插入數(shù)據(jù)
    sheet.Cells[iRow, iCol] = value; // 左上角第一格的坐標(biāo)是[1, 1]
b. 按塊插入數(shù)據(jù)
    object[,] objVal = new object[Height, Length];
    // 設(shè)置數(shù)據(jù)塊
    Excel.Range range = sheet.get_Range(sheet.Cells[iRow, iCol], sheet.Cells[iRow + Height, iCol + Length])
    range.Value2 = objVal;

 

 

 

4. 清理內(nèi)存和恢復(fù)環(huán)境

    System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
    while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excel) > 0) ;
    range = null;
    sheet = null;
    book = null;
    excel = null;
    GC.Collect();
    System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;

 

 

------------------------

 

特別是第二點(diǎn)。好有用!

 

 

 

 

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
根據(jù)Excel線程句柄得到ID并且關(guān)閉進(jìn)程
VSTO學(xué)習(xí)筆記(五)批量編輯Excel 2010 x64
兩招搞定C#讀取Excel文件
如何比較兩個(gè)EXCEL 文件的不同(各個(gè)EXCEL版本的方法)
[c#]結(jié)束excel的進(jìn)程的方法
C#導(dǎo)出EXCEL 并解決EXCEL進(jìn)程問題!
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服