使用过滤器更改 SaveFileDialog 行为
Changing SaveFileDialog behaviour with filters
我必须按一下按钮,以不同的扩展名导出一些数据。为此,我提供了一个 SaveFileDialog
,但根据过滤器(以及输出文件扩展名),我必须使用不同的过程写入数据:
- 如果我必须将其导出为
.txt
或 .csv
,我必须使用 writer
- 否则,如果我必须将其导出为
.xlsx
、.xls
或 .ods
,我必须使用 NPOI
我为作家和 NPOI 提供了代码,它们单独工作。如果我尝试将它们放在一起,.txt
和 .csv
可以工作,但对于 .xlsx
,它会导出损坏的文件。
这是我正在使用的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt|CSV Files (*.csv*)|*.csv|Excel 2010 Workbook (*.xlsx*)|*.xlsx|Excel 2000 Workbook (*.xls*)|*.xls|OpenOffice Spreadsheet (*.ods*)|*.ods"
SaveFileDialog1.CheckFileExists = False
Dim t As Integer
t = CInt(Form10.Label13.Text)
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
If SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt|CSV Files (*.csv*)|*.csv|" Then
Using writer = New StreamWriter(SaveFileDialog1.FileName)
For Each o As Object In Form10.ListBox2.Items
writer.WriteLine(o)
Next
End Using
End If
End If
If SaveFileDialog1.Filter = "Excel 2010 Workbook (*.xlsx*)|*.xlsx|Excel 2000 Workbook (*.xls*)|*.xls|OpenOffice Spreadsheet (*.ods*)|*.ods|" Then
Dim fs As New FileStream(SaveFileDialog1.FileName, FileMode.Create, FileAccess.Write)
Dim workbook As IWorkbook = New XSSFWorkbook()
Dim worksheet As ISheet = workbook.CreateSheet()
Dim ich As ICreationHelper = workbook.GetCreationHelper()
Dim rows As New List(Of IRow)
Dim rowz As IRow = worksheet.CreateRow(0)
rowz.CreateCell(0).SetCellValue(ich.CreateRichTextString("Time [s]"))
rowz.CreateCell(1).SetCellValue(ich.CreateRichTextString("HRR [kW]"))
For i As Integer = 1 To t
Dim row As IRow = worksheet.CreateRow(i)
row.CreateCell(0).SetCellValue(CDbl(Form10.ListBox1.Items(i)))
row.CreateCell(1).SetCellValue(CDbl(Form10.ListBox2.Items(i)))
rows.Add(row)
Next i
workbook.Write(fs)
fs.Close()
End If
End If
End Sub
我想知道是否为过滤器正确设置了“if 循环”。谢谢大家会回答我。最好的问候。
只需获取用户选择的文件路径并检查其扩展名,例如
Select Case Path.GetExtension(SaveFileDialog1.FileName)
Case ".txt", ".csv"
'...
Case ".xlsx", ".xls", ".ods"
'...
End Select
我必须按一下按钮,以不同的扩展名导出一些数据。为此,我提供了一个 SaveFileDialog
,但根据过滤器(以及输出文件扩展名),我必须使用不同的过程写入数据:
- 如果我必须将其导出为
.txt
或.csv
,我必须使用 writer - 否则,如果我必须将其导出为
.xlsx
、.xls
或.ods
,我必须使用 NPOI
我为作家和 NPOI 提供了代码,它们单独工作。如果我尝试将它们放在一起,.txt
和 .csv
可以工作,但对于 .xlsx
,它会导出损坏的文件。
这是我正在使用的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt|CSV Files (*.csv*)|*.csv|Excel 2010 Workbook (*.xlsx*)|*.xlsx|Excel 2000 Workbook (*.xls*)|*.xls|OpenOffice Spreadsheet (*.ods*)|*.ods"
SaveFileDialog1.CheckFileExists = False
Dim t As Integer
t = CInt(Form10.Label13.Text)
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
If SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt|CSV Files (*.csv*)|*.csv|" Then
Using writer = New StreamWriter(SaveFileDialog1.FileName)
For Each o As Object In Form10.ListBox2.Items
writer.WriteLine(o)
Next
End Using
End If
End If
If SaveFileDialog1.Filter = "Excel 2010 Workbook (*.xlsx*)|*.xlsx|Excel 2000 Workbook (*.xls*)|*.xls|OpenOffice Spreadsheet (*.ods*)|*.ods|" Then
Dim fs As New FileStream(SaveFileDialog1.FileName, FileMode.Create, FileAccess.Write)
Dim workbook As IWorkbook = New XSSFWorkbook()
Dim worksheet As ISheet = workbook.CreateSheet()
Dim ich As ICreationHelper = workbook.GetCreationHelper()
Dim rows As New List(Of IRow)
Dim rowz As IRow = worksheet.CreateRow(0)
rowz.CreateCell(0).SetCellValue(ich.CreateRichTextString("Time [s]"))
rowz.CreateCell(1).SetCellValue(ich.CreateRichTextString("HRR [kW]"))
For i As Integer = 1 To t
Dim row As IRow = worksheet.CreateRow(i)
row.CreateCell(0).SetCellValue(CDbl(Form10.ListBox1.Items(i)))
row.CreateCell(1).SetCellValue(CDbl(Form10.ListBox2.Items(i)))
rows.Add(row)
Next i
workbook.Write(fs)
fs.Close()
End If
End If
End Sub
我想知道是否为过滤器正确设置了“if 循环”。谢谢大家会回答我。最好的问候。
只需获取用户选择的文件路径并检查其扩展名,例如
Select Case Path.GetExtension(SaveFileDialog1.FileName)
Case ".txt", ".csv"
'...
Case ".xlsx", ".xls", ".ods"
'...
End Select