Excel 导出指定行为txt文件(VBA,宏)
要从Excel 多个sheet内导出指定行为txt文件,懒得用C#了,写个VBA宏
Sub Export()
Dim FileName As Variant
Dim Sep As String
Dim StartSheet As Integer
Dim EndSheet As Integer Dim ExportIndex As Integer '文件名
FileName = Application.GetSaveAsFilename(InitialFileName:=vbNullString, FileFilter:="Text Files (*.txt),*.txt")
If FileName = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
'分隔符
' Sep = Application.InputBox("Enter a separator character.", Type:=2) '开始Sheet
'StartSheet = Application.InputBox("开始Sheet.", Type:=2)
'结束Sheet
EndSheet = Application.InputBox("结束Sheet.", Type:=) '导出行
ExportIndex = Application.InputBox("导出行号.", Type:=) ShartSheet:=StartSheet, EndSheet:=EndSheet, ExportRow:=ExportIndex
ExportRangeToTextFile FName:=CStr(FileName), SelectionOnly:=False, AppendData:=False, _
ShartSheet:=, EndSheet:=EndSheet, ExportRow:=ExportIndex
End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 将Excel内多个Sheet中的某一行导出Text
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportRangeToTextFile(FName As String, _
SelectionOnly As Boolean, _
AppendData As Boolean, ShartSheet As Integer, _
EndSheet As Integer, ExportRow As Integer) Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
Dim X As Variant Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile
Open FName For Output Access Write As #FNum For i = To Application.sheets.Count
X = Application.sheets(i).UsedRange.Value
WholeLine = ""
With Application.sheets(i).UsedRange
StartRow = .Cells().Row
StartCol = .Cells().Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With For j = To EndCol
WholeLine = WholeLine + X(ExportRow, j) + Chr("") '\t
Next
Print #FNum, WholeLine
Next
MsgBox "OK" '
EndMacro:
On Error GoTo
Application.ScreenUpdating = True
Close #FNum
'XT = Application.Transpose(X)转置 End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 导出单个sheet
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportSingleSheetToTextFile(FName As String, _
Sep As String, SelectionOnly As Boolean, _
AppendData As Boolean) Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile If SelectionOnly = True Then
With Selection
StartRow = .Cells().Row
StartCol = .Cells().Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
Else
With ActiveSheet.UsedRange
StartRow = .Cells().Row
StartCol = .Cells().Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
End If If AppendData = True Then
Open FName For Append Access Write As #FNum
Else
Open FName For Output Access Write As #FNum
End If For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = Chr() & Chr()
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
Print #FNum, WholeLine
Next RowNdx EndMacro:
On Error GoTo
Application.ScreenUpdating = True
Close #FNum End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 将Excel内多个Sheet中的某一行导出New Sheet
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportRangeToNewSheet(FName As String, _
SelectionOnly As Boolean, _
AppendData As Boolean, ShartSheet As Integer, _
EndSheet As Integer, ExportRow As Integer)
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String
Dim X As Variant
Dim Xsheet As Worksheet Set Xsheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
Xsheet.Name = FName 'Format(Now(), "HHmmss") Application.ScreenUpdating = False Dim index As Integer
index =
'For i = 1 To Application.Sheets.Count
For i = ShartSheet To EndSheet 'Application.Sheets.Count
With Application.Sheets(i).UsedRange
EndCol = .Cells(.Cells.Count).Column
For j = To EndCol
Xsheet.Cells(j, * index - ).Value = .Cells(, j).Text
Xsheet.Cells(j, * index).Value = .Cells(ExportRow, j).Text
Next
End With
index = index +
Next
MsgBox "导出OK,Sheet名" + FName '
'XT = Application.Transpose(X)转置 End Sub
//从text文件导入Excel sheet里面
Sub OpenFile() Dim filter As String
Dim fileToOpen filter = "All Files(*.*),*.*,Word Documents(*.do*),*.do*," & _
"Text Files(*.txt),*.txt"
fileToOpen = Application.GetOpenFilename(filter, 4, "请选择文件") If fileToOpen = False Then
MsgBox "你没有选择文件", vbOKOnly, "提示"
Else ' Workbooks.Open FileName:=fileToOpen
' MsgBox "你选择的文件是:" & fileToOpen, vbOKOnly, "提示"
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" + fileToOpen, Destination:=Range("$A$1") _
)
.Name = "Sample"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
End Sub
vba: Importing text file into excel sheet
http://blog.csdn.net/ldwtill/article/details/8571781
Using a QueryTable Sub Sample()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Sample.txt", Destination:=Range("$A$1") _
)
.Name = "Sample"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Open the text file in memory Sub Sample()
Dim MyData As String, strData() As String Open "C:\Sample.txt" For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)
End Sub
Once you have the data in the array you can export it to the current sheet. Using the method that you are already using Sub Sample()
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet Set wbI = ThisWorkbook
Set wsI = wbI.Sheets("Sheet1") '<~~ Sheet where you want to import Set wbO = Workbooks.Open("C:\Sample.txt") wbO.Sheets(1).Cells.Copy wsI.Cells wbO.Close SaveChanges:=False
End Sub
FOLLOWUP You can use the Application.GetOpenFilename to choose the relevant file. For example... Sub Sample()
Dim Ret Ret = Application.GetOpenFilename("Prn Files (*.prn), *.prn") If Ret <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Ret, Destination:=Range("$A$1")) '~~> Rest of the code End With
End If
End Sub
Excel 导出指定行为txt文件(VBA,宏)的更多相关文章
- 导出OpenID为txt文件的方法
导出OpenID为txt文件的方法 public function export(){ $shop = M("Shop"); $arr = $shop->field('ope ...
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
功能说明 输入文件路径,在浏览器输出文件预览信息,经测试极速(Chrome).IE9.Firefox通过 分类文件及代码说明 DemoFiles 存放可测试文件 Default.aspx 启动页 ...
- excel、csv、txt文件数据读取
/// <summary> /// 读取Excel表每一行第一列的字符串集合 /// </summary> /// <param name="filePath& ...
- winform NPOI excel 导出并选择保存文件路径
public void ExcelOp(DataGridView gdv,ArrayList selHead) { if (selHead.Count==0) { MessageBox.Show(&q ...
- SVN导出指定版本差异文件 ***
当一个项目进入运营维护阶段以后,不会再频繁地更新全部源文件到服务器,这个时间的修改大多是局部的,因此更新文件只需更新修改过的文件,其他 没有修改过的文件就没有必要上载到服务器.但一个稍微上规模的项目文 ...
- SAS 读取指定目录下文件列表宏
OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...
- mysql 导出行数据到txt文件,指定字符分割
select id,name, concat('tel:',phone) from user order by time INTO outfile 'user.txt' FIELDS terminat ...
- dataview将excel表格的数据导出成txt文件
有时候需要处理大量的数据,且这些数据又存在于excel表格内,在平时的时候,我是非常喜欢这样的数据的,因为只要是excel表格内的数据,处理起来的方法就很方便.也可能我平时遇见的数据总是以一种杂乱无章 ...
- 将指定目录中的txt文件转化成excel文件
#!/usr/bin/env python#coding:utf-8import reimport osimport globimport xlwtimport sysdir=r"F:\te ...
随机推荐
- 使用ehCache作为本地缓存
package nd.sdp.basic.config; import org.springframework.cache.CacheManager; import org.springframewo ...
- ASP.NET MVC Core的TagHelper(基础篇)
TagHelper又是一个新的名词,它替代了自之前MVC版本的HtmlHelper,专注于在cshmlt中辅助生成html标记. 通过使用自定义的TagHelper可以提供自定义的Html属性或元素, ...
- C#中区别多态、重载、重写
重写是指重写基类的方法,在基类中的方法必须有修饰符virtual,而在子类的方法中必须指明override. 格式: 基类中: public virtual void myMethod() { } 子 ...
- Echarts 使用asp.net +ashx+ajax 实现 饼图、柱形图后台交互
向上效果图 前端code /* * ------------------------------------------------------------------ * module-inform ...
- 查看SQLServer数据库表占用大小
查看数据库mdf 文件中 各个表数据占用大小. create table #t(name varchar(255), rows bigint, reserved varchar(20), ...
- 撩课-Python-每天5道面试题-第2天
一. 简述编程过程中, 注释的作用? (1) 方便开发人员自己理清楚代码思路 因为开发人员在拿到一个需求时, 首先应该思考的是如何将需求问题, 分解成具体的实施步骤; 第一步干啥, 第二步干啥, 第三 ...
- Spring MVC No converter found for return value of type 解决方法
1.在pom中添加 jackson <properties> <jackson.version>2.8.5</jackson.version> </prope ...
- Qless 相关知识
Qless是一个基于redis的分布式任务架构.相关代码在 https://github.com/seomoz/qless 它是完全有lua实现的,依靠 redis 对lua的支持,http://ww ...
- Struts2 (四) — 拦截器
一.拦截器 1.概述 1.1什么是拦截器 在struts2中,拦截器(Interceptor)是用来动态拦截Action执行的对象. 拦截器有点类似以前Servlet阶段的Filter(过滤器 ...
- [HNOI2006]最短母串问题
题目大意:给定一个字符串集,求一个最短字串,使得该集合内的串都是该串的一个子串 算法:AC自动机+最短路+状压DP 注意空间限制 #include"cstdio" #include ...