我做了一點研究,以及不確定我是否應該使用C#在Excel中保存.csv後,我該如何打開它?
中的Microsoft.Office.Interop.Excel
剛剛與例如澄清:我正在一個.txt ,並做些東西,然後將其保存爲.CSV(逗號分隔值)。我想然後打開它(按鈕點擊或...)
這怎麼可以做?
我做了一點研究,以及不確定我是否應該使用C#在Excel中保存.csv後,我該如何打開它?
中的Microsoft.Office.Interop.Excel
剛剛與例如澄清:我正在一個.txt ,並做些東西,然後將其保存爲.CSV(逗號分隔值)。我想然後打開它(按鈕點擊或...)
這怎麼可以做?
對你的評論:
我希望它在EXCEL.EXE打開。這應該是我的程序與它的轉換完成後
使用System.Diagnostics.Process
類它只是啓動啓動一個單獨的窗口:
using System.Diagnostics.Process;//at top of your application
//
//At button click or after you finish editing
//
Process excel = new Process();
//if the excel was installed in the target machine and the default program to open csvs
//then you can simply just call process start and put the file path, like:
excel.Start(@"Your edited csv file path");
//otherwise:
excel.StartInfo.FileName = @"The excel application file path";
excel.StartInfo.Arguments = @"Your edited csv file path";
excel.Start();
是的,Microsoft.Office.Interop.Excel
是你需要在Excel中打開CSV文件。
這是一個WinForms,WPF或ASP.NET應用程序嗎?如果使用Office Interop庫,則依賴於正在該機器上安裝的Office。如果它是一個Web服務器,你不想以這種方式運行Excel。
查看www.SpreadSheetGear.com。沒有從屬關係,只是非常滿意。
Windows窗體。謝謝。 – wizlog
您可以使用文件路徑作爲命令行參數(excel.exe C:\ myFile.csv)來啓動excel進程。這將在excel中打開它。
取決於您使用的框架(即Silverlight或Windows Forms)。 如果我是你,我會使用OpenFileDialog從逗號分隔列表中讀取字符串或類的值。以下示例適用於silverlight。
private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
{
// Create an instance of the open file dialog box.
var openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "CSV File (.csv)|*.csv|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
// Open the selected file to read.
System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
{
// Read the first line from the file and write it the textbox.
tbResults.Text = reader.ReadLine();
//the results of your CSV are now stored in tbResults.Text
//optionally you could parse the .CSV using string.Spit(',') into a string array
}
fileStream.Close();
}
}
它的Windows窗體。而我並沒有試圖讀取CSV。我創建了CSV,我只是試圖打開它。 – wizlog
可能重複http://stackoverflow.com/questions/4652621/c -excel-interop-opening-and-showing-csv-file – Zenwalker
用什麼打開它?在你的應用程序,在單獨的過程.. –
http://stackoverflow.com/questions/906841/csv-parser-reader-for-c – dtanders