Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello,

I'm using this code to open exel file and write, but it works only those file which are already created.
I want to create a new excel and then save my data.

myExcelApp = new Excel.ApplicationClass();
myExcelApp.Visible = false;
myExcelWorkbooks = myExcelApp.Workbooks;

SaveFileDialog save = new SaveFileDialog();
save.Filter = "Excel Files .xlsx|*.xlsx";

save.ShowDialog();
string sss = save.FileName;
String fileName = sss;

myExcelWorkbook = myExcelWorkbooks.Open(fileName, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
Excel.Worksheet myExcelWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;

String cellFormulaAsString = myExcelWorksheet.get_Range("A1", misValue).Formula.ToString();

string s;
int i = 1;

foreach (ListViewItem li in listView2.Items)
{
    s = "A" + i.ToString();

    myExcelWorksheet.get_Range(s, misValue).Formula = li.Text;
    i++;
}

myExcelWorkbooks.Close()
Posted
Updated 18-Mar-11 12:08pm
v4
Comments
[no name] 18-Mar-11 8:02am    
Edited for Code block.
situ21 18-Mar-11 8:04am    
how help me by example
TweakBird 18-Mar-11 9:06am    
May i know your application in Winforms or web(Asp.net)?
Dalek Dave 18-Mar-11 18:09pm    
Edited for Readability.

1 solution

Before you close your workbooks, you should call Workbook.SaveAs on the workbook(s) you want to save. Here's the documentation:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(v=vs.80).aspx[^]
 
Share this answer
 
Comments
Dalek Dave 18-Mar-11 18:09pm    
Good Link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900