如何使用winform中的savedialogbox将现有excel文件保存为其他名称 [英] how to save existing excel file as different name using savedialogbox in winform
问题描述
我的代码是
我正在导入xml文件并在此按钮上单击我在excel文件中转换,但我想使用对话框将其名称更改为新名称
这个点击
private void btnExport_Click(object sender,EventArgs e)
{
if(dataGridView1.Rows.Count> 0)
{
试试
{
DataTable dt = new DataTable();
foreach(dataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col .HeaderText,col.ValueType);
}
int count = 0;
foreach(DataGridView.Rows中的DataGridViewRow行)
{
if(count< dataGridView1.Rows.Count - 1)
{
dt.Rows.Add();
foreach(row.Cells中的DataGridViewCell单元格)
{
dt.Rows [dt.Rows.Count - 1] [cell.ColumnIndex] = cell.Value.ToString();
}
}
count ++;
}
StreamWriter wr = new StreamWriter(@D:\\ Book3.xls);
for(int i = 0;我< dt.Columns.Count; i ++)
{
wr.Write(dt.Columns [i] .ToString()。ToUpper()+\ t);
}
wr.WriteLine();
for(int i = 0; i<(dt.Rows.Count); i ++)
{
for(int j = 0; j< dt.Columns.Count; j ++)
{
if( dt.Rows [i] [j]!= null)
{
wr.Write(Convert.ToString(dt.Rows [i] [j])+ \t);
}
其他
{
wr.Write(\t );
}
}
wr.WriteLine();
}
wr.Close();
label1.Visible = true;
label1.Text =数据导出成功;
//按其他名称保存FileName Book3.xls ////
Process.Start(新ProcessStartInfo(excel.exe) ,@D:\\Book3.xls));
if(System.IO.File.Exists(@D:\\ Book3.xls))
{
saveFileDialog1.ShowDialog();
saveFileDialog1.InitialDirectory = @D:\;
saveFileDialog1 .CheckFileExists = true;
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.DefaultExt =xls;
saveFileDialog1.Filter =Excel File | * .xls; *,xlsx;
saveFileDialog1.RestoreDirector y = true;
}
}
catch(例外情况)
{
抛出前;
}
}
My code is
I am importing xml file and on this button click i am converting in excel file ,but i want to change its name to new name using dialogbox
on this click
private void btnExport_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
try
{
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.HeaderText, col.ValueType);
}
int count = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (count < dataGridView1.Rows.Count - 1)
{
dt.Rows.Add();
foreach (DataGridViewCell cell in row.Cells)
{
dt.Rows[dt.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();
}
}
count++;
}
StreamWriter wr = new StreamWriter(@"D:\\Book3.xls");
for (int i = 0; i < dt.Columns.Count; i++)
{
wr.Write(dt.Columns[i].ToString().ToUpper() + "\t");
}
wr.WriteLine();
for (int i = 0; i < (dt.Rows.Count); i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j] != null)
{
wr.Write(Convert.ToString(dt.Rows[i][j]) + "\t");
}
else
{
wr.Write("\t");
}
}
wr.WriteLine();
}
wr.Close();
label1.Visible = true;
label1.Text = "Data Exported Successfully";
//Saving FileName Book3.xls by other name////
Process.Start(new ProcessStartInfo("excel.exe",@"D:\\Book3.xls"));
if (System.IO.File.Exists(@"D:\\Book3.xls"))
{
saveFileDialog1.ShowDialog();
saveFileDialog1.InitialDirectory = @"D:\";
saveFileDialog1.CheckFileExists = true;
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.DefaultExt = "xls";
saveFileDialog1.Filter = "Excel File|*.xls;*,xlsx";
saveFileDialog1.RestoreDirectory = true;
}
}
catch (Exception ex)
{
throw ex;
}
}
推荐答案
您以错误的方式使用SaveFileDialog
。在调用ShowDialog
之前,需要设置其中的所有参数。然后在返回时,如果用户单击了确定
,则会从对话框中获取文件名。您还需要添加代码以实际保存文件。
You are using theSaveFileDialog
in the wrong way. You need to set all the parameters in it before you callShowDialog
. Then on return if the user has clickedOK
, you get the filename out of the dialog. You also need to add the code to do the actual saving of the file.
这篇关于如何使用winform中的savedialogbox将现有excel文件保存为其他名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!