Saving can be done by using the SaveAs method of the WorkBook class. Making Excel invisible can be done by setting the Application Visible property to false.
The SaveAs method has many parameters. Because they are optional you have not the supply them all. In the code below I have only supplied the filename (1st parameter) and the fileformat (2nd parameter). The example assume you have Excel 2007 (using XlFileFormat.xlExcel12 format).
Also take notice of the xla.Quit(); xla = null; statements. Don't forget these. Otherwise Excel will keep running in the background.
private string GetFileName()
{
string filename = string.Empty;
SaveFileDialog dialog = new SaveFileDialog();
dialog.InitialDirectory = @"C:\";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
filename = dialog.FileName;
}
return filename;
}
private void SaveWorkbook(Microsoft.Office.Interop.Excel.Workbook wb, string filename)
{
string localfilename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(filename), System.IO.Path.GetFileNameWithoutExtension(filename));
if (System.IO.File.Exists(localfilename))
System.IO.File.Delete(localfilename);
wb.SaveAs(localfilename, XlFileFormat.xlExcel12);
}
private void SaveButton_Click(object sender, EventArgs e)
{
string filename = GetFileName();
if (string.IsNullOrEmpty(filename))
return;
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;
int i = 1;
int j = 1;
foreach (ListViewItem comp in listView_playlist.Items)
{
ws.Cells[i, j] = comp.Text.ToString();
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
{
ws.Cells[i, j] = drv.Text.ToString();
j++;
}
j = 1;
i++;
}
SaveWorkbook(wb, filename);
xla.Quit();
xla = null;
MessageBox.Show(string.Format("Data is save to {0}", filename));
}
(improvement)
CSV has the advantage that you could use it on any system. To help you I have made some code to show how to save and load from a CSV file. The examples are quite basic and make some assumption. It will give you a start. If you want to learn more about CSV file (for example using the first line to store the field names, or making it more robust) you could search the CodeProject articles for CSV. There are som could articles here. Both basic and advanced.
To save your playlist to a CSV you could use the following code. To make the example work you need
using System.IO;
using System.Text.RegularExpressions;
private void NewSaveButton_Click(object sender, EventArgs e)
{
string filename = GetFileName();
if (string.IsNullOrEmpty(filename))
return;
if (Path.GetExtension(filename).Length == 0)
filename = filename + ".csv";
WriteListViewToCsv(listView_playlist, filename, '|');
}
private void WriteListViewToCsv(ListView listview, string csvFilename, char seperator)
{
int i = 1;
int j = 1;
using (StreamWriter sw = new StreamWriter(csvFilename, false))
{
foreach (ListViewItem comp in listview.Items)
{
bool firstItem = true;
foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
{
if (firstItem)
firstItem = false;
else
sw.Write(seperator);
sw.Write(drv.Text.ToString());
j++;
}
sw.WriteLine();
j = 1;
i++;
}
sw.Flush();
sw.Close();
}
}
To read the saved playlist from the CSV file you could use the follow code. It uses regex to split the line.
private void NewLoad_Click(object sender, EventArgs e)
{
string filename = OpenFileName();
if (string.IsNullOrEmpty(filename))
return;
ImportCSV(listView_playlist, filename, '|');
}
private string OpenFileName()
{
string filename = string.Empty;
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = @"C:\";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
filename = dialog.FileName;
}
return filename;
}
private void ImportCSV(ListView listview, string filename, char seperator)
{
using (StreamReader sr = new StreamReader(filename))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] values = Regex.Split(line, @"\|");
ListViewItem item = listview.Items.Add(values[0]);
if (values.Count() > 1)
{
for (int c = 1; c < values.Count(); c ++)
{
item.SubItems.Add(values[c]);
}
}
}
sr.Close();
}
}