传递OpenFileDialog和SaveFileDialog之间的文件名称
我有一个WPF应用程序,它没有编译错误,但仍然无法正常工作,我的代码有什么问题?
简而言之是在问题标题中描述,但它应该提示用户打开一个Excel文件,然后按另一个button应运行exportExcelToTxt方法,并提供一个保存文件对话框。 打开的文件对话框起作用,当我按下另一个button时,它会给出一个保存文件对话框,但是在我select它的时候不会生成一个文件。
我的代码:
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : System.Windows.Window { public MainWindow() { InitializeComponent(); } private void BtnFileOpen_Click(object sender, RoutedEventArgs e) { var fileDialog = new System.Windows.Forms.OpenFileDialog(); var result = fileDialog.ShowDialog(); switch (result) { case System.Windows.Forms.DialogResult.OK: var file = fileDialog.FileName; TxtFile.Text = file; TxtFile.ToolTip = file; break; case System.Windows.Forms.DialogResult.Cancel: default: TxtFile.Text = null; TxtFile.ToolTip = null; break; } } private void convert_Click(object sender, RoutedEventArgs e) { // Configure save file dialog box Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = "Document"; // Default file name dlg.DefaultExt = ".txt"; // Default file extension dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension // Show save file dialog box Nullable<bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document string filename = dlg.FileName; } } static void exportExcelToTxt(string excelFilePath, string outputTxtPath) { Dictionary<string, List<long>> values = new Dictionary<string, List<long>>(); using (OleDbConnection excelConnection = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0;HDR=YES", excelFilePath))) { excelConnection.Open(); string firstSheet = getFirstSheetName(excelConnection); using (OleDbCommand cmd = excelConnection.CreateCommand()) { cmd.CommandText = string.Format("SELECT * FROM [{0}]", firstSheet); using (OleDbDataAdapter da = new OleDbDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { da.Fill(dt); // Getting all the data in the sheet foreach (DataRow item in dt.Rows) { List<long> toAdd = new List<long>(); string key = item[0] as string; for (int i = 1; i < dt.Columns.Count; i++) { toAdd.Add(Convert.ToInt64(item[i])); } values.Add(key, toAdd); // Associating all the "numbers" to the "Name" } } } } } StringBuilder toWriteToTxt = new StringBuilder(); foreach (KeyValuePair<string, List<long>> item in values) { // Formatting the output toWriteToTxt.Append(string.Format("{0}:", item.Key)); foreach (long val in item.Value.Distinct()) { toWriteToTxt.AppendFormat("\t{0} * {1}\r\n", item.Value.Where(f => f == val).Count(), // Amount of occurrencies of each number val); } } // Writing the TXT using (FileStream fs = new FileStream(outputTxtPath, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(toWriteToTxt.ToString()); } } } static string getFirstSheetName(OleDbConnection excelConnection) { using (DataTable ExcelTables = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" })) { return ExcelTables.Rows[0]["TABLE_NAME"].ToString(); } } } }
if (result == true) { // Save document string filename = dlg.FileName; }
这将从对话框中检索选定的文件名,但由于filename
是本地variables而不能立即丢失,因此您不要调用save / export方法。
加
if (result == true) { // Save document string filename = dlg.FileName; exportExcelToTxt(TxtFile.Text, filename); }