急急急,加急!如何用perl将txt文件写到Excel里面

不知有晋 2012-08-18 09:06:47
现在有1000多个txt数据文件,我想把他们写到Excel文件里,我记得perl可以做到,有谁知道怎么做吗?涉及到那些模块,在哪里可以学到,麻烦各位大神了!
...全文
244 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gloveing 2012-08-19
  • 打赏
  • 举报
回复
#========================================================
# 2012/07/31 23:18:22
#========================================================
from win32com.client import Dispatch
import time
api = Dispatch('Excel.Application')
work_book = api.Workbooks.Open(r'C:\Users\ago\Desktop\test.xls')
try:
work_sheet = work_book.Worksheets
'''
#创建单元格内容
for i in range(1,27):
for j in range(1,27):
ch = 65 + j -1
ch = chr(ch) + str(i)
work_sheet(1).Cells(i,j).Value = ch
work_book.Save()
'''
m_range = work_sheet(1).Range('A1:Z26').Find('B15')#查找单元格内容为'B15'的range
for i in m_range:
print i #输出单元格内容为'B15'的Cell

work_book.Close()
except Exception,e:
print e
work_book.Save()
work_book.Close()
Gloveing 2012-08-19
  • 打赏
  • 举报
回复
#========================================================
# 注:在运行之前先得创建D:/screenshot.bmp以及D:/1.xls
# 2012/07/29 19:37:50
#========================================================
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from win32com.client import Dispatch
import win32com.client
class easyExcel:
"""A utility to make it easier to get at Excel. Remembering
to save the data is your problem, as is error handling.
Operates on one workbook at a time."""
def __init__(self, filename=None):
self.xlApp = win32com.client.Dispatch('Excel.Application')
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''

def save(self, newfilename=None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
def close(self):
self.xlBook.Close(SaveChanges=0)
del self.xlApp
def getCell(self, sheet, row, col):
"Get value of one cell"
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row, col).Value
def setCell(self, sheet, row, col, value):
"set value of one cell"
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row, col).Value = value
def getRange(self, sheet, row1, col1, row2, col2):
"return a 2d array (i.e. tuple of tuples)"
sht = self.xlBook.Worksheets(sheet)
return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
def addPicture(self, sheet, pictureName, Left, Top, Width, Height):
"Insert a picture in sheet"
sht = self.xlBook.Worksheets(sheet)
sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)

def cpSheet(self, before):
"copy sheet"
shts = self.xlBook.Worksheets
shts(1).Copy(None,shts(1))

if __name__ == "__main__":
PNFILE = r'D:/screenshot.bmp'
xls = easyExcel(r'D:/1.xls')
xls.addPicture('Sheet1', PNFILE, 20,20,1000,1000)
xls.cpSheet('Sheet1')
xls.save()
xls.close()
Gloveing 2012-08-19
  • 打赏
  • 举报
回复
#========================================================
# 2012/07/31 23:18:22
# 查找
#========================================================
from win32com.client import Dispatch
import time
api = Dispatch('Excel.Application')
work_book = api.Workbooks.Open(r'C:\Users\ago\Desktop\test.xls')
try:
work_sheet = work_book.Worksheets
'''
#创建单元格内容
for i in range(1,27):
for j in range(1,27):
ch = 65 + j -1
ch = chr(ch) + str(i)
work_sheet(1).Cells(i,j).Value = ch
work_book.Save()
'''
m_range = work_sheet(1).Range('A1:Z26').Find('B15')#查找单元格内容为'B15'的range
for i in m_range:
print i #输出单元格内容为'B15'的Cell

work_book.Close()
except Exception,e:
print e
work_book.Save()
work_book.Close()
不知有晋 2012-08-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

中文 Excel 读写终级解决方案
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1626493&extra=page%3D1%26filter%3Ddigest%26digest%3D1%26digest%3D1
[/Quote]先谢后看,谢谢!
不知有晋 2012-08-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

反正要是用COM啦。。
我只有python的,行不?
[/Quote]python的也好 啊,队友学过,发来瞧瞧啊!谢谢!
不知有晋 2012-08-19
  • 打赏
  • 举报
回复
谢谢各位,我好像做出来了!
#!usr/bin/perl
#print"Hello";
use strict;
use Win32;
use Cwd;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...

# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
use Encode;

my $outexcel1="C:/Documents and Settings/Owner/桌面/第三次模拟/第三次模拟/B/附件/a.xls";

#for($i=1;$i<=500;$i++){



my $Book = $Excel->Workbooks->Open("$outexcel1") or die "Could not open the $outexcel1 !!!\n";
my $Sheet = $Book->Worksheets(1);
my $row=0;
my %hangye;
my %num;
my %word;
my %word1;


my $i=1;
while($i<=500){
my $infile1="$i.txt";

open IN1,"$infile1"
or die "could not open the input file!";


while(my $aline=<IN1>)
{
$row++;
print $row."\n";
$aline=~s/\n$//;
my @array=split(/\t/,$aline);

$Sheet->Cells($row,1)->{'Value'}=$array[0];
$Sheet->Cells($row,2)->{'Value'}=$array[1];

$Sheet->Cells($row,3)->{'Value'}=$array[2];
$Sheet->Cells($row,4)->{'Value'}=$array[3];#$big.":".$name{$big};
$Sheet->Cells($row,5)->{'Value'}=$array[4];
$Sheet->Cells($row,6)->{'Value'}=$array[5];
$Sheet->Cells($row,7)->{'Value'}=$array[6];
$Sheet->Cells($row,8)->{'Value'}=$array[7];
$Sheet->Cells($row,9)->{'Value'}=$array[8];
$Sheet->Cells($row,10)->{'Value'}=$array[9];
$Sheet->Cells($row,11)->{'Value'}=$array[10];
$Sheet->Cells($row,12)->{'Value'}=$array[11];
$Sheet->Cells($row,13)->{'Value'}=$array[12];
$Sheet->Cells($row,14)->{'Value'}=$array[13];
$Sheet->Cells($row,15)->{'Value'}=$array[14];
$Sheet->Cells($row,16)->{'Value'}=$array[15];
$Sheet->Cells($row,17)->{'Value'}=$array[16];
$Sheet->Cells($row,18)->{'Value'}=$array[17];
$Sheet->Cells($row,19)->{'Value'}=$array[18];
}
$i++;
}
$Book->Close;
不过发现excel存不了那么多数据!
Gloveing 2012-08-18
  • 打赏
  • 举报
回复
反正要是用COM啦。。
我只有python的,行不?

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧