vb.net操作excel檔案

來源:互聯網
上載者:User

  要求將資料顯示在excel表中,並且要設好資料格式。 雖說如此,真正實現了自己的功能,還是費了不少時間。相信還是有很多與我相似的人需要實現這種功能。那就做個小結吧。不妥之處,請指正哦。

  1.添加引用--com---microsoft excel 11.0 object library

  2在需要訪問excel的過程中定義

dim exapp as excel.application  '定義excel應用程式
dim exbook as excel.workbook ‘定義活頁簿
dim exsheet as excel.worksheet ‘定義工作表
dim exrange as excel.range   '定義工作區域

  3.有了上面的定義,基本上excel的操作就手到擒來了

  exapp.visible=true  '顯示excel 程式
  exbook=exapp.workbooks.add()  '添加新活頁簿  或exbook=exapp.workbooks.open("薄名")  / 開啟已在工作薄
  exsheet=exbook.sheets(n)   '獲得第n個工作表的控制控制代碼,後面就由它處理了
  exsheet.cells(row,col)=值    '對指定儲存格賦值,這個操作大量出現哦
  exsheet.range(cells,cells).font       '這個屬性也常用到,設定格式就是它了:
  exsheet.range(cells,cells).font.colorindex=3  '設定顏色
exsheet.range(cells,cells).Borders.LineStyle=1'設邊框線
exsheet.range(cells,cells).EntireColumn.AutoFit()  '自動列寬  /可以改為自動行高哦
exsheet.range(cells,cells).merge         '合併儲存格,複雜的表格這個也用的多哦
exsheet.range(cells,cells).HorizontalAlignment=3  '水平置中  ,當然也可以改為垂直的
exSheet.PageSetup.Orientation = 2   '頁面橫向

  熟練以上這些,基本上就可以實現一些要求了。

  4。最後是儲存檔案

  exsheet.saveas("檔案名稱")

  或

  exbook.save()

  5.清理變數

exsheet=nothing
exbook.close
exbook=nothing
exapp.quit
exapp=nothing

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.