当前位置: 首页 > wzjs >正文

如何优化网站结构十堰城市建设网站

如何优化网站结构,十堰城市建设网站,库尔勒网站建设,创建网站站点(VBA实现Excel转csv) 初步学习了VBA相关的知识后,解决了一个需求: 要求读取指定xlsx文件中的指定sheet页,将该sheet页的内容转换为csv文件。 实现的布局如下所示: 文章目录 ①实现从指定行开始全数据转换为…

(VBA实现Excel转csv)
初步学习了VBA相关的知识后,解决了一个需求:
要求读取指定xlsx文件中的指定sheet页,将该sheet页的内容转换为csv文件。
实现的布局如下所示:
在这里插入图片描述

文章目录

  • ①实现从指定行开始全数据转换为csv
  • ②实现指定行指定列部分数据转换为csv

①实现从指定行开始全数据转换为csv

1、Select-File 按钮功能实现
Select-File 按钮主要是实现选取文件,并将选取文件路径显示到下方。

Private Sub CommandButton1_Click()Dim filePath As StringfilePath = SelectFile()If filePath <> "" ThenTextBox1.Text = filePathElseTextBox1.Text = "Please select file丅"End If
End SubPublic Function SelectFile(Optional title As String = "Select File") As StringWith Application.FileDialog(msoFileDialogFilePicker).title = title.AllowMultiSelect = False.Filters.Clear.Filters.Add "all file", "*.*"If .Show = -1 ThenSelectFile = .SelectedItems(1)ElseSelectFile = ""End IfEnd With
End Function

2、Output-File 按钮功能实现
Output-File 按钮功能:读取选取文件的指定sheet页,将对应内容转换为csv文件进行保存,并将保存文件的路径显示在下方。

Private Sub CommandButton3_Click()On Error GoTo ErrorHandlerDim sourceFilePath As StringDim wbSource As WorkbookDim wsSource As WorksheetDim savePath As StringDim fso As ObjectDim ts As ObjectDim lastRow As Long, lastCol As LongDim i As Long, j As LongDim csvContent As StringDim dataArr() As VariantDim startRow As LongDim fileName As String, folderPath As StringDim timeStamp As StringDim targetCols As VariantDim colIndex As VariantDim colCounter As LongDim tempArr As VariantDim rowData As VarianttargetCols = Array(1, 3, 5, 6, 7)Dim colCount As LongcolCount = UBound(targetCols) - LBound(targetCols) + 1startRow = 4sourceFilePath = Trim(TextBox1.Text)If sourceFilePath = "" ThenMsgBox "Please select the file丅", vbExclamationExit SubEnd IfIf Dir(sourceFilePath) = "" ThenMsgBox "File does not exit, please select again", vbCriticalExit SubEnd IfSet fso = CreateObject("Scripting.FileSystemObject")fileName = "Test11111.csv"fileExt = fso.GetExtensionName(sourceFilePath)folderPath = fso.GetParentFolderName(sourceFilePath)savePath = folderPath & "\" & fileNameTextBox2.Text = savePathIf Dir(savePath) <> "" ThenIf MsgBox("The file already exists, do you want to overwrite it?", vbQuestion + vbYesNo) = vbNo ThenExit SubEnd IfEnd IfApplication.ScreenUpdating = FalseSet wbSource = Workbooks.Open(sourceFilePath, ReadOnly:=True)Set wsSource = wbSource.Sheets("Sheet1")With wsSourcelastRow = .Cells(.Rows.Count, 1).End(xlUp).RowactualLastCol = .Cells(startRow, .Columns.Count).End(xlToLeft).ColumnmaxCol = Application.Max(targetCols)If lastRow < startRow ThenMsgBox "Data does not exits丅", vbExclamationGoTo CleanUpEnd IftempArr = .Range(.Cells(startRow, 1), .Cells(lastRow, maxCol)).ValueDim rowCount As LongrowCount = UBound(tempArr, 1)ReDim dataArr(1 To rowCount, 1 To colCount)For i = 1 To rowCountcolCounter = 1For Each colIndex In targetColsIf colIndex <= UBound(tempArr, 2) ThendataArr(i, colCounter) = tempArr(i, colIndex)ElsedataArr(i, colCounter) = ""End IfcolCounter = colCounter + 1Next colIndexNext iEnd WithSet ts = fso.CreateTextFile(savePath, True, False)For i = 1 To UBound(dataArr, 1)csvContent = ""For j = 1 To UBound(dataArr, 2)csvContent = csvContent & CleanCSVValue(dataArr(i, j))If j < UBound(dataArr, 2) Then csvContent = csvContent & ","Next jts.WriteLine csvContentNext its.CloseMsgBox "CSV file:" & vbCrLf & savePath, vbInformation, "has outputed"
CleanUp:If Not wbSource Is Nothing ThenwbSource.Close SaveChanges:=FalseSet wbSource = NothingEnd IfSet ts = NothingSet fso = NothingSet wsSource = NothingApplication.ScreenUpdating = TrueExit SubErrorHandler:MsgBox "Error happend" & Err.Description, vbCritical, "ERROR"Resume CleanUp
End Sub
Private Function CleanCSVValue(ByVal inputValue As Variant) As StringIf IsEmpty(inputValue) Or IsNull(inputValue) ThenCleanCSVValue = ""Exit FunctionEnd IfDim result As Stringresult = CStr(inputValue)If InStr(result, ",") > 0 Or InStr(result, """") > 0 Or InStr(result, vbCr) > 0 Or InStr(result, vbLf) > 0 Thenresult = Replace(result, """", """""")result = """" & result & """"End IfCleanCSVValue = result
End Function

3、Clear 按钮功能实现
Clear 按钮主要是将下方两个文件路径清空。

Private Sub CommandButton2_Click()TextBox1.Text = ""TextBox2.Text = ""
End Sub

实现的效果如图所示:
在这里插入图片描述

②实现指定行指定列部分数据转换为csv

1、Select-File 按钮功能实现
3、Clear 按钮功能实现
这两个按钮的实现和①一致,没有区别,关键不同点在于转换部分。
2、Output-File 按钮功能实现
Output-File 按钮功能:读取选取文件的指定sheet页,将 指定的列对应内容转换为csv文件进行保存,并将保存文件的路径显示在下方。

Private Sub CommandButton3_Click()On Error GoTo ErrorHandlerDim sourceFilePath As StringDim wbSource As WorkbookDim wsSource As WorksheetDim savePath As StringDim fso As ObjectDim ts As ObjectDim lastRow As Long, lastCol As LongDim i As Long, j As LongDim csvContent As StringDim dataArr() As VariantDim startRow As LongDim fileName As String, folderPath As StringDim timeStamp As StringDim targetCols As VariantDim colIndex As VariantDim colCounter As LongDim tempArr As VariantDim rowData As VarianttargetCols = Array(1, 3, 5, 6, 7)Dim colCount As LongcolCount = UBound(targetCols) - LBound(targetCols) + 1startRow = 4sourceFilePath = Trim(TextBox1.Text)If sourceFilePath = "" ThenMsgBox "请选择文件。", vbExclamationExit SubEnd IfIf Dir(sourceFilePath) = "" ThenMsgBox "文件不存在,请重新选择。", vbCriticalExit SubEnd IfSet fso = CreateObject("Scripting.FileSystemObject")fileName = "11111.csv"fileExt = fso.GetExtensionName(sourceFilePath)folderPath = fso.GetParentFolderName(sourceFilePath)savePath = folderPath & "\" & fileNameTextBox2.Text = savePathIf Dir(savePath) <> "" ThenIf MsgBox("文件已存在,是否覆盖", vbQuestion + vbYesNo) = vbNo ThenExit SubEnd IfEnd IfApplication.ScreenUpdating = FalseSet wbSource = Workbooks.Open(sourceFilePath, ReadOnly:=True)Set wsSource = wbSource.Sheets("Sheet1")With wsSourcelastRow = .Cells(.Rows.Count, 1).End(xlUp).RowactualLastCol = .Cells(startRow, .Columns.Count).End(xlToLeft).ColumnmaxCol = Application.Max(targetCols)If lastRow < startRow ThenMsgBox "文件没有数据", vbExclamationGoTo CleanUpEnd IftempArr = .Range(.Cells(startRow, 1), .Cells(lastRow, maxCol)).ValueDim rowCount As LongrowCount = UBound(tempArr, 1)ReDim dataArr(1 To rowCount, 1 To colCount)For i = 1 To rowCountcolCounter = 1For Each colIndex In targetColsIf colIndex <= UBound(tempArr, 2) ThendataArr(i, colCounter) = tempArr(i, colIndex)ElsedataArr(i, colCounter) = ""End IfcolCounter = colCounter + 1Next colIndexNext iEnd WithSet ts = fso.CreateTextFile(savePath, True, False)For i = 1 To UBound(dataArr, 1)csvContent = ""For j = 1 To UBound(dataArr, 2)csvContent = csvContent & CleanCSVValue(dataArr(i, j))If j < UBound(dataArr, 2) Then csvContent = csvContent & ","Next jts.WriteLine csvContentNext its.CloseMsgBox "CSV文件:" & vbCrLf & savePath, vbInformation, "转换完成"
CleanUp:If Not wbSource Is Nothing ThenwbSource.Close SaveChanges:=FalseSet wbSource = NothingEnd IfSet ts = NothingSet fso = NothingSet wsSource = NothingApplication.ScreenUpdating = TrueExit SubErrorHandler:MsgBox "错误发生" & Err.Description, vbCritical, "ERROR"Resume CleanUp
End Sub
Private Function CleanCSVValue(ByVal inputValue As Variant) As StringIf IsEmpty(inputValue) Or IsNull(inputValue) ThenCleanCSVValue = ""Exit FunctionEnd IfDim result As Stringresult = CStr(inputValue)If InStr(result, ",") > 0 Or InStr(result, """") > 0 Or InStr(result, vbCr) > 0 Or InStr(result, vbLf) > 0 Thenresult = Replace(result, """", """""")result = """" & result & """"End IfCleanCSVValue = result
End Function

文章转载自:

http://AzyERmVp.jcjgh.cn
http://TwY6SRNz.jcjgh.cn
http://Zixi8CRT.jcjgh.cn
http://TpgKCpjs.jcjgh.cn
http://gPuEa0kQ.jcjgh.cn
http://bAVTm5ht.jcjgh.cn
http://j3anK5Nc.jcjgh.cn
http://CaZ7WXll.jcjgh.cn
http://uibP0SLf.jcjgh.cn
http://fj7yAXdM.jcjgh.cn
http://OkMsGTft.jcjgh.cn
http://iozVmMEX.jcjgh.cn
http://UuIpXtUM.jcjgh.cn
http://dxlMBL1B.jcjgh.cn
http://POJYGpty.jcjgh.cn
http://M4hkac4I.jcjgh.cn
http://1W180o6y.jcjgh.cn
http://pjna6vww.jcjgh.cn
http://vtiQHLNi.jcjgh.cn
http://tm3g0bmG.jcjgh.cn
http://DDmK4Had.jcjgh.cn
http://vGP3SN9U.jcjgh.cn
http://Zokf3Egm.jcjgh.cn
http://11FCsak0.jcjgh.cn
http://pcyjir4q.jcjgh.cn
http://kPLpSXQ9.jcjgh.cn
http://dMEfjS3K.jcjgh.cn
http://5U2KMQpS.jcjgh.cn
http://a4Y0K0JS.jcjgh.cn
http://7kqqLBkM.jcjgh.cn
http://www.dtcms.com/wzjs/712620.html

相关文章:

  • 如何给网站配色谁做彩票网站代理
  • 网站优化的意义怎么使用电脑是做网站
  • 克拉玛依市建设局网站wordpress 微博模板
  • 网站做推广怎么收费建设游戏网站需要哪些设备
  • 建设工作室网站网站建设开题报告书
  • 网站标题关键词描述深圳创业补贴咨询电话
  • 怎么看一个网站是否是外包做的destoon做的网站
  • 网站如何添加图标我想建设一个算命网站
  • 上海空灵网站设计北京装修公司哪家口碑最好 知乎
  • 不用下载就能看的网站的浏览器高性能网站建设书籍
  • 网站开发定义名称网站占有率
  • 国际英文网站广州网站搭建快速提升网站排名
  • 济南做网站公司wordpress 会员中心插件
  • 荣成市城乡建设局网站创建网页用什么软件
  • 定制版网站建设详细报价单网站建设 解决方案
  • 虚拟主机做网站wordpress3.7.1下载
  • 大良网站建设如何网站备案初审时间
  • 拍卖网站功能需求文档wordpress 文字插件下载
  • 免费素材网站素材库简单的网页设计论文
  • 我需要把网站做山东城市建设职业学院教务网站
  • 做网站虚拟主机和云服务器吗北京网站后台培训
  • 单页网站 营销wordpress登入地址
  • 赌场网站建站网站建设方案开发
  • 开发区网站建设工作管理办法网页设计与制作教程目录
  • 有人找做网站的led灯网站策划书
  • sns社交网站开发教程在线制作头像模板
  • 微网站是免费的吗wordpress文章自动标签
  • 西安知名的网站建设公司wordpress 主题 36kr
  • 深圳最专业的高端网站建设阿里云虚拟主机wordpress建站教程
  • 网站图文列表公司网络架构