简体   繁体   English

如何在不询问 VB.NET 的情况下覆盖 SaveAs 上的文件

[英]How to Overwrite file on SaveAs without asking VB.NET

I am creating an exporter to export the data I make from some Queries to csv files.我正在创建一个导出器来将我从一些Queries中生成的数据导出到csv文件。

My problem is that I am generating many csv, and my program asks me for each of the files to see if I want to overwrite the previous one.我的问题是我正在生成许多 csv,我的程序要求我提供每个文件以查看是否要覆盖前一个文件。

Is there any way to overwrite them without having to ask me all the time?有什么方法可以覆盖它们而不必一直问我吗?

Dim excel As Object
Dim workbook As Object
Dim sheet As Object

excel = CreateObject("Excel.Application")
workbook = excel.Workbooks.Open("C:\Users\me\Desktop\NECESARY.xlsx")

'I complete all I want to export here

workbook.SaveAs("C:\Users\me\Desktop\" & file & ".csv")
workbook.Close()

This works correctly, and exports all my data perfectly, but as told, I need not to be asked if I want to overwrite the file, there are too many files for going one by one accepting them.这可以正常工作,并且可以完美地导出我的所有数据,但正如我所说,我不需要被问到是否要覆盖文件,有太多文件需要一个一个地接受它们。

One solution is to delete the previous file before saving:一种解决方案是在保存之前删除以前的文件:

Dim excel As Object
Dim workbook As Object
Dim sheet As Object

excel = CreateObject("Excel.Application")
workbook = excel.Workbooks.Open("C:\Users\me\Desktop\NECESARY.xlsx")

'I complete all I want to export here

Dim fileName As String = "C:\Users\me\Desktop\" & file & ".csv"

System.IO.File.Delete(fileName)     

workbook.SaveAs(fileName)
workbook.Close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM