Update Z1 export to use common functions
This commit is contained in:
@@ -110,3 +110,93 @@ Function ReadCSVFile(ByVal filePath As String) As Variant
|
||||
|
||||
ReadCSVFile = Split(textContent, vbLf)
|
||||
End Function
|
||||
|
||||
' ============================================================
|
||||
' Get save file path for CSV
|
||||
' Returns: file path or "" if cancelled
|
||||
' ============================================================
|
||||
Function GetSaveCSVPath(Optional ByVal defaultName As String = "") As String
|
||||
Dim savePath As String
|
||||
savePath = Application.GetSaveAsFilename( _
|
||||
FileFilter:="CSV Files (*.csv), *.csv", _
|
||||
Title:="Save CSV", _
|
||||
InitialFileName:=defaultName)
|
||||
|
||||
If savePath = "False" Or savePath = "" Then
|
||||
GetSaveCSVPath = ""
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If InStr(1, savePath, ".csv", vbTextCompare) = 0 Then
|
||||
savePath = savePath & ".csv"
|
||||
End If
|
||||
|
||||
GetSaveCSVPath = savePath
|
||||
End Function
|
||||
|
||||
' ============================================================
|
||||
' Write content to CSV file with Shift-JIS encoding
|
||||
' ============================================================
|
||||
Sub WriteCSVFile(ByVal filePath As String, ByVal content As String)
|
||||
Dim stream As Object
|
||||
Set stream = CreateObject("ADODB.Stream")
|
||||
With stream
|
||||
.Type = 2
|
||||
.Charset = "shift_jis"
|
||||
.Open
|
||||
.WriteText content, 1
|
||||
.SaveToFile filePath, 2
|
||||
.Close
|
||||
End With
|
||||
End Sub
|
||||
|
||||
' ============================================================
|
||||
' Build CSV content from worksheet
|
||||
' Parameters:
|
||||
' ws - worksheet
|
||||
' startRow - data start row
|
||||
' endRow - data end row
|
||||
' dataColumns - array of column numbers to export
|
||||
' Returns: CSV content string
|
||||
' ============================================================
|
||||
Function BuildCSVContent(ByVal ws As Worksheet, ByVal startRow As Long, ByVal endRow As Long, ByRef dataColumns() As Long, Optional ByVal headerRow As Long = 0) As String
|
||||
Dim csvContent As String
|
||||
Dim r As Long
|
||||
Dim col As Long
|
||||
Dim firstCol As Boolean
|
||||
|
||||
' Build header if specified
|
||||
If headerRow > 0 Then
|
||||
For col = LBound(dataColumns) To UBound(dataColumns)
|
||||
If col = LBound(dataColumns) Then
|
||||
csvContent = Trim(ws.Cells(headerRow, dataColumns(col)).Value)
|
||||
Else
|
||||
csvContent = csvContent & "," & Trim(ws.Cells(headerRow, dataColumns(col)).Value)
|
||||
End If
|
||||
Next col
|
||||
csvContent = csvContent & vbLf
|
||||
End If
|
||||
|
||||
' Build data rows
|
||||
For r = startRow To endRow
|
||||
If Len(Trim(ws.Cells(r, dataColumns(LBound(dataColumns))).Value & "")) > 0 Then
|
||||
firstCol = True
|
||||
For col = LBound(dataColumns) To UBound(dataColumns)
|
||||
If firstCol Then
|
||||
csvContent = csvContent & CleanCSVField(ws.Cells(r, dataColumns(col)).Value)
|
||||
firstCol = False
|
||||
Else
|
||||
csvContent = csvContent & "," & CleanCSVField(ws.Cells(r, dataColumns(col)).Value)
|
||||
End If
|
||||
Next col
|
||||
csvContent = csvContent & vbLf
|
||||
End If
|
||||
Next r
|
||||
|
||||
' Trim trailing newlines
|
||||
Do While Right(csvContent, 1) = vbLf
|
||||
csvContent = Left(csvContent, Len(csvContent) - 1)
|
||||
Loop
|
||||
|
||||
BuildCSVContent = csvContent
|
||||
End Function
|
||||
|
||||
Reference in New Issue
Block a user