我有一个应用程序,用户在其中与数据库交互并准备他需要什么的报告。商品价格列在数据库中。一旦他选择了项目,他就会生成 Excel 格式的报告。当他点击 Generate a Report 按钮时,系统会询问他将报告保存在哪里。这些都很好。问题在于,当他保存报告并点击“生成报告”按钮时,当该位置存在同名文件时,他能够将同名文件保存在同一位置。它基本上取代了旧的。我写了一个代码来检查文件是否存在并且它可以工作,但问题是它不会在你保存文件的那一刻出现。之前出现过。我想要的是当我尝试将文件保存在任何位置时,它会在那一刻给我“
如果有人需要任何澄清,请发表评论,我大部分时间都在线。
这是代码
private void btnRunReport_Click(object sender, EventArgs e)
{
if (cmbReportsList.SelectedIndex != -1)
{
_qry = new QueryMgt();
OpenProjectDb();
string _sProjectName = (dbManager.ExecuteScalar(CommandType.Text, _qry.GetProjectFileName())).ToString();
CloseProjectDb();
if (cmbReportsList.SelectedIndex == 0)
{
SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx";
SaveFile.DefaultExt = "xlsx";
SaveFile.FileName = _sProjectName + " Report1";
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }
btnRunReport.Visible = false;
bg1804Rpt.RunWorkerAsync();
Application.DoEvents();
}
}
else if (cmbReportsList.SelectedIndex == 1)
{
SaveFile.FileName = _sProjectName + " Report2";
SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx";
SaveFile.DefaultExt = "xlsx";
if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//SaveFile.FileName = "Report2";
this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }
btnRunReport.Visible = false;
bgMCRpt.RunWorkerAsync();
Application.DoEvents();
}
}
else if (cmbReportsList.SelectedIndex == 2)
{
_qry = new QueryMgt();
_formula = new FormulaMgt();
string sSQL;
string chrCountryName = "";
OpenProjectDb();
OpenTableDb();
DataSet dsProjectSpec;
DataSet dsCountry;
sSQL = _qry.GetProjectSpec().ToString();
dsProjectSpec = dbManager.ExecuteDataSet(CommandType.Text, sSQL);
sSQL = _qry.GetCountry().ToString();
dsCountry = dbManagerTable.ExecuteDataSet(CommandType.Text, sSQL);
foreach (DataRow row in dsCountry.Tables[0].Select("pk_lngCountryID ='" + dsProjectSpec.Tables[0].Rows[0]["lngCountryId"] + "'"))
{
chrCountryName = row["chrCountryName"].ToString();
break;
}
SaveFile.FileName = _sProjectName + " Report3";
SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx";
SaveFile.DefaultExt = "xlsx";
// SaveFile.Filter = "Microsoft Office Excel Worksheet (*.xlsx)|*.xls|All files (*.*)|*.*";
if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//SaveFile.FileName = "Report3";
this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }
btnRunReport.Visible = false;
bgMDRpt.RunWorkerAsync();
Application.DoEvents();
}
}
}
else if (cmbReportsList.SelectedIndex == -1)
{
MessageBox.Show("Please select any report", "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}