C# true,5,“,”,true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0); private void tlpMenuItem_S
true,5,“,”,true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0);C# true,5,“,”,true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0); private void tlpMenuItem_S,c#,excel,c#-4.0,C#,Excel,C# 4.0,true,5,“,”,true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0); private void tlpMenuItem_SaveAs_Click(object sender, EventArgs e) { string sd; svFileDialog_SaveButton.ShowDialog(); /
private void tlpMenuItem_SaveAs_Click(object sender, EventArgs e)
{
string sd;
svFileDialog_SaveButton.ShowDialog();
//saveFileDialog1.InitialDirectory = "c:";
svFileDialog_SaveButton.Filter = "Excel File|*.xlsx|All Files|*.*";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 20;
sd = svFileDialog_SaveButton.FileName;
ExcelApp.ActiveWorkbook.SaveCopyAs(sd + ".xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Excel file created");
}
private void OpenFile()
{
string findFile = "";
xlexcel = new Excel.Application();
xlexcel.Visible = true;
// Open a File
xlWorkBook = xlexcel.Workbooks.Open("C:\MyFile.xlsx", 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Username";
xlWorkSheet.Cells[1, 2] = "Password";
xlWorkSheet.Cells[1, 3] = "Warehouse Location";
xlWorkSheet.Cells[1, 4] = "Date";
}
private void tlpMenuItem_SaveAs_Click(object sender, EventArgs e)
{
string sd;
svFileDialog_SaveButton.ShowDialog();
//saveFileDialog1.InitialDirectory = "c:";
svFileDialog_SaveButton.Filter = "Excel File|*.xlsx|All Files|*.*";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 20;
sd = svFileDialog_SaveButton.FileName;
ExcelApp.ActiveWorkbook.SaveCopyAs(sd + ".xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Excel file created");
}
private void OpenFile()
{
string findFile = "";
xlexcel = new Excel.Application();
xlexcel.Visible = true;
// Open a File
xlWorkBook = xlexcel.Workbooks.Open("C:\MyFile.xlsx", 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Username";
xlWorkSheet.Cells[1, 2] = "Password";
xlWorkSheet.Cells[1, 3] = "Warehouse Location";
xlWorkSheet.Cells[1, 4] = "Date";
}
private void SaveDataToAFile()
{
int _lastRow = xlWorkSheet.Range["A" + xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1;
xlWorkSheet.Cells[_lastRow, 1] = txt_Username.Text;
xlWorkSheet.Cells[_lastRow, 2] = txt_Password.Text;
xlWorkSheet.Cells[_lastRow, 3] = cmb_DatabaseSelection.SelectedIndex.ToString();
xlWorkSheet.Cells[_lastRow, 4] = DateTime.Now;
}
private void CloseFile()
{
xlWorkBook.Close(true, misValue, misValue);
xlexcel.Quit();
ReleaseObject(xlWorkSheet);
ReleaseObject(xlWorkBook);
ReleaseObject(xlexcel);
}
private void ReleaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
String filepath; //global variable
Stream stream;
SaveFileDialog sf = new SaveFileDialog();
if(sf.ShowDialog() == true)
{
if((stream = sf.OpenFile()) != null)
{
filepath = sf.FileName;
//Do save work
}
}