日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

VB中操作Excel文档

發布時間:2023/12/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VB中操作Excel文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

???????篇一:利用 Python 操作 Excel文檔

利用 Python 操作 Excel

在Excel 2010中打開VBA編輯器,請按 Alt + F11 打開; 以下代碼在 Excel 2007, Excel 2010中測試通過;

import win32com.client

# 導入腳本模塊

ExcelApp = win32com.client.Dispatch("Excel.Application") # 載入EXCEL模塊 ExcelApp.Visible = True

1、新建xls文件

wBook = ExcelApp.Workbooks.Add() 立3個空表

nSheet = wBook.Worksheets.Count wSheet = wBook.Worksheets(1)

# 獲取文件的表格數,缺省為3 # 打開指定工作表(注意序號從1開始) # 增加新表,新表為第4個表,相當與

# 新建空文件,每個文件系統默認建

# 顯示EXCEL應用程序

wSheet = wBook.Worksheets.Add() wBook.Worksheets(4) wSheet.Name = "新建表格"

# 修改新建表格名稱,

或者wBook.Worksheets(4).Name = "新建表格" ……中間操作…… wBook.SaveAs(strName) wBook.Save() wBook.Close()

2、打開和關閉xls文件

xlsPathName = r"E:\000.xls"

# 另存文件,注意直接保存用:

# 關閉文件(賬本)

# 指定路徑名

# 打開指定文件(賬本)

wBook = ExcelApp.Workbooks.Open(xlsPathName) wSheet = wBook.Worksheets(1)

# 打開指定工作表(注意序號從1開始)

# 必須使用準確的工作表名稱字

或者wSheet = wBook.Worksheets("Sheet1") 符串

……中間操作…… wBook.Close()

# 關閉文件(賬本)

3、頁面設置

wSheet.PageSetup.PaperSize = 9

# 設置紙張大小,A3=8,A4=9(與Word不同)

# 設置頁面方向,縱向=1,橫向=2(與

wSheet.PageSetup.Orientation = 1Word不同)

wSheet.PageSetup.TopMargin = 3*28.35wSheet.PageSetup.BottomMargin = 3*28.35

# 頁邊距上=3cm,1cm=28.35pt # 頁邊距下=3cm # 頁邊距左=2.5cm # 頁邊距右=2.5cm

wSheet.PageSetup.LeftMargin = 2.5*28.35 wSheet.PageSetup.RightMargin = 2.5*28.35

wSheet.PageSetup.CenterHorizontally = True wSheet.PageSetup.CenterVertically = False后一頁不好看)

wSheet.PageSetup.HeaderMargin = 2*28.35 wSheet.PageSetup.FooterMargin = 1*28.35 邊)

wSheet.PageSetup.PrintTitleRows = "$1:$2"

# 表格打印位置水平居中

# 表格打印位置垂直不居中(最

# 設置頁眉位置=2cm(距上邊)

# 設置頁腳位置=1cm(距下

# 設置表格標題行

wSheet.PageSetup.CenterHeader = "&\"黑體\"&15表格名稱" # 中央頁眉作為表格名稱 wSheet.PageSetup.CenterFooter = "第&P 頁,共&N 頁" # 中央頁腳顯示打印頁數

wSheet.Rows(5).PageBreak = -4135

注意:表格名稱的文本格式設置,詳情查詢“頁眉和頁腳的格式代碼”

&"黑體"&15 &B&I&U &L&C&R

4、單元格操作

# 設置字體,字大,顏色

# 在第5行之前插入分頁符

# 設置字體加黑、加粗、下劃線 # 設置左中右對齊

cv = wSheet.Cells(1, 1).Value # 獲取單元格數值

# 設置單元格背景色

wSheet.Cells(1, 1).Interior.Color = 0xff00ff cel = wSheet.Cells(2, 2)

# 獲取單元格對象

# 獲取偏移后的單元格,即(4,4)

cv = cel.Offset(3, 3).Value

# Offset以當前單元格為(1,1),偏移之后的單元格為(a1+a2-1, b1+b2-1)

注:range與Cells, Rows, Columns的絕大多數屬性類似,但是后三者

后面可以接序號(2,3)表示具體單元格或行列,不接序號指所有單元格或行列。

5、行列操作

wSheet.Rows.AutoFit()

# 自動適合行

# 刪除第1行

wSheet.Rows(1).Delete()

wSheet.Columns.Autofit() wSheet.Columns(1).Delete()

# 自動適合列

# 刪除第1列

# 設置列寬 # 設置數值格式

wSheet.Columns(1).Columnwidth = 30

wSheet.Columns(1).NumberFormatLocal = "000000"

wSheet.Rows("2:2").Select()

# 必須選擇第2行,才能凍結第1行!!

# 凍結第1行

ExcelApp.ActiveWindow.FreezePanes = True

6、遍歷工作表的所有單元格

nRow = wSheet.usedrange.rows.count

# 獲取指定工作表的行數 # 獲取指定工作表的列數

nCol = wSheet.usedrange.columns.count for i in range(1, nRow+1): for j in range(1, nCol+1): ……中間操作……break break

# 注意必須分別退出

7、搜索指定數值

cel = wSheet.Columns(2).Find(123) if(cel):

adr = cel.Address while(True): 反復搜索

……中間操作……

cel = wSheet.Columns(2).FindNext(cel) if(cel.Address==adr): break

7、格式設置

wSheet.Cells.Font.Name = "Arial" wSheet.Cells.Font.Size = 10 wSheet.Cells.Font.Bold = True

# 設置字體 # 設置字大 # 設置粗體 # 設置斜體

# 設置所有列自動調整寬度

# 設置左對齊(1=兩端, 2=左, 3=

# 注意FindNext()參數為cel

# 獲取該單元格首地址,以便退出循環 # 注意系統的FindNext()是個死循環,會

# 在第2列查找某整數值123

# 返回到單元格首地址時,就退出

wSheet.Cells.Font.Italic = True wSheet.usedrange.Columns.AutoFit

wSheet.Cells.HorizontalAlignment = 2 中, 4=右)

ran = wSheet.Range(wSheet.Cells(1,1),wSheet.Cells(nRow,nCol)) ran.Hyperlinks.Delete()ran.Font.Name = "宋體" ran.Font.Size = 10

# 刪除指定范圍的鏈接

# 設置字體

# 設置字大 # 設置黑體

# 設置斜體 # 水平對齊 # 垂直對其

ran.Font.Bold = False ran.Font.Italic = True

ran.HorizontalAlignment = -4108 ran.VerticalAlignment = -4108

8、繪制表格線

ran = wSheet.Range("A1:D5")

# 設置處理范圍

ran.Borders.LineStyle = 1 條,-4119=雙線,-4115虛線)

# 設置線型為實線(1=實線,4=點劃線,-4142=無線

ran.Borders(11).LineStyle = -4142 # 去除范圍內中間豎線(8=上邊線,9=下邊線,

7=左框線,10=右框線,11=豎框線,12=橫框線)

9、行列寬度設置

wSheet.Rows(1).RowHeight = 20 wSheet.Columns(1).ColumnWidth = 10

10、使用公式進行統計

ran = wSheet.Range("A1:A10")

# 設置計算范圍

# 范圍數值求和 # 范圍最大值

# 設置第1行的行高

# 設置第1列的列寬

a = ExcelApp.WorksheetFunction.Sum(ran) b = ExcelApp.WorksheetFunction.Max(ran)

注:常用函數可以在插入函數對話框中查找,包括 Average, Max, Min, Sum, StDev等;

篇二:python表單提交

#!/usr/bin/python

#-*-coding:utf-8-*-

# 進行表單提交 小項 2008-10-09

import httplib,urllib; #加載模塊

#定義需要進行發送的數據

params = urllib.urlencode({'cat_id':'6',

'news_title':'標題-Test39875',

'news_author':'Mobedu',

'news_ahome':'來源',

'tjuser':'carchanging',

'news_keyword':'|',

'news_content':'測試-Content',

'action':'newnew',

'MM_insert':'true'});

#定義一些文件頭

headers = {"Content-Type":"application/x-www-form-urlencoded",

"Connection":"Keep-Alive","Referer":"http://192.168.1.212/newsadd.asp?action=newnew"};#與網站構建一個連接

conn = httplib.HTTPConnection("192.168.1.212");

#開始進行數據提交同時也可以使用get進行

conn.request(method="POST",url="/newsadd.asp?action=newnew",body=params,headers=headers);

#返回處理后的數據

response = conn.getresponse();

#判斷是否提交成功

if response.status == 302:

print "發布成功!^_^!";

else:

print "發布失敗\^0^/";

#關閉連接

conn.close();

篇三:python經典實例

1 輸出你好 #打開新窗口,輸入:

#! /usr/bin/python

# -*- coding: utf8 -*-

s1=input("Input your name:")

print("你好,%s" % s1)

'''

知識點:

* input("某字符串")函數:顯示"某字符串",并等待用戶輸入.

* print()函數:如何打印.

* 如何應用中文

* 如何用多行注釋

'''

2 輸出字符串和數字

但有趣的是,在javascript里我們會理想當然的將字符串和數字連接,因為是動態語言嘛.但在Python里有點詭異,如下:

#! /usr/bin/python

a=2

b="test"

c=a+b 運行這行程序會出錯,提示你字符串和數字不能連接,于是只好用內置函數進行轉換 #! /usr/bin/python

#運行這行程序會出錯,提示你字符串和數字不能連接,于是只好用內置函數進行轉換 a=2

b="test"

c=str(a)+b

d="1111"

e=a+int(d)

#How to print multiply values

print ("c is %s,e is %i" % (c,e))

'''

知識點:

* 用int和str函數將字符串和數字進行轉換

* 打印以#開頭,而不是習慣的//

* 打印多個參數的方式

'''

3 列表 #! /usr/bin/python

# -*- coding: utf8 -*-

#列表類似Javascript的數組,方便易用

#定義元組

word=['a','b','c','d','e','f','g']

#如何通過索引訪問元組里的元素

a=word[2]

print ("a is: "+a)

b=word[1:3]

print ("b is: ")

print (b) # index 1 and 2 elements of word.

c=word[:2]

print ("c is: ")

print (c) # index 0 and 1 elements of word.

d=word[0:]

print ("d is: ")

print (d) # All elements of word.

#元組可以合并

e=word[:2]+word[2:]

print ("e is: ")

print (e) # All elements of word.

f=word[-1]

print ("f is: ")

print (f) # The last elements of word.

g=word[-4:-2]

print ("g is: ")

print (g) # index 3 and 4 elements of word.

h=word[-2:]

print ("h is: ")

print (h) # The last two elements.

i=word[:-2]

print ("i is: ")

print (i) # Everything except the last two characters

l=len(word)

print ("Length of word is: "+ str(l))

print ("Adds new element")

word.append('h')

print (word)

#刪除元素

del word[0]

print (word)

del word[1:3]

print (word)

'''

知識點:

* 列表長度是動態的,可任意添加刪除元素. * 用索引可以很方便訪問元素,甚至返回一個子列表

* 更多方法請參考Python的文檔

'''

4 字典 #! /usr/bin/python

x={'a':'aaa','b':'bbb','c':12}

print (x[(來自: 小龍 文檔 網:python,網頁表格)9;a'])

print (x['b'])

print (x['c'])

for key in x:

print ("Key is %s and value is %s" % (key,x[key]))

'''

知識點:

* 將他當Java的Map來用即可.

'''

5 字符串 比起C/C++,Python處理字符串的方式實在太讓人感動了.把字符串當列表來用吧. #! /usr/bin/python

word="abcdefg"

a=word[2]

print ("a is: "+a)

b=word[1:3]

print ("b is: "+b) # index 1 and 2 elements of word.

c=word[:2]

print ("c is: "+c) # index 0 and 1 elements of word.

d=word[0:]

print ("d is: "+d) # All elements of word.

e=word[:2]+word[2:]

print ("e is: "+e) # All elements of word.

f=word[-1]

print ("f is: "+f) # The last elements of word.

g=word[-4:-2]

print ("g is: "+g) # index 3 and 4 elements of word.

h=word[-2:]

print ("h is: "+h) # The last two elements.

i=word[:-2]

print ("i is: "+i) # Everything except the last two characters

l=len(word)

print ("Length of word is: "+ str(l)) 中文和英文的字符串長度是否一樣?

#! /usr/bin/python

# -*- coding: utf8 -*-

s=input("輸入你的中文名,按回車繼續");

print ("你的名字是 : " +s)

l=len(s)

print ("你中文名字的長度是:"+str(l))

知識點:

? 類似Java,在python3里所有字符串都是unicode,所以長度一致.

6 條件和循環語句

#! /usr/bin/python

#條件和循環語句

x=int(input("Please enter an integer:"))

if x<0:

x=0

print ("Negative changed to zero")

elif x==0:

print ("Zero")

else:

print ("More")

# Loops List

a = ['cat', 'window', 'defenestrate']

for x in a:

print (x, len(x))

#知識點:

# * 條件和循環語句

# * 如何得到控制臺輸入

7 函數

#! /usr/bin/python

# -*- coding: utf8 -*-

def sum(a,b):

return a+b

func = sum

r = func(5,6)

print (r)

# 提供默認值

def add(a,b=2):

return a+b

r=add(1)

print (r)

r=add(1,5)

print (r)

一個好用的函數

#! /usr/bin/python

# -*- coding: utf8 -*-

# The range() function

a =range (1,10)

for i in a:

print (i)

a = range(-2,-11,-3) # The 3rd parameter stands for step for i in a:

print (i)

知識點:

? Python 不用{}來控制程序結構,他強迫你用縮進來寫程序,使代碼清晰. ? 定義函數方便簡單

? 方便好用的range函數

8 異常處理

#! /usr/bin/python

s=input("Input your age:")

if s =="":

raise Exception("Input must no be empty.")

try:

i=int(s)

except Exception as err:

print(err)

總結

以上是生活随笔為你收集整理的VB中操作Excel文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。