Hi All
i have excel file in my project i need to make a copy for that & i need to save that copied file with some name..
can anybody tell me how can i do this.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AartiRank #2,383 · 594 reputationPosted Jan 5, 2012, 1:40 AM EST
Try with below code: I hope it will help you.
Example:
string strFileName = "TemplateFile.xls";
string strFilePath = LocationPath + strFileName;
//Create the directory and Delete the if the file already exists.
CreateDirandDelFile(LocationPath, strFileName);
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
oXL = new Excel.Application();
oXL.Visible = false;
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
oSheet.Name = cboTempName.SelectedItem.Text.ToString().Trim();
for (int i = 0; i < dtToExprt.Columns.Count; i++)
{
//First Row
oSheet.Cells[1, i+1] = dtToExprt.Rows[0][i].ToString().Trim();
((Range)oSheet.Cells[1, i+1]).ColumnWidth = 20;
((Range)oSheet.Cells[1, i+1]).EntireColumn.WrapText = true;
((Range)oSheet.Cells[1, i + 1]).EntireRow.Font.Size = 8;
((Range)oSheet.Cells[1, i + 1]).EntireRow.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
//Second Row
oSheet.Cells[2, i+1] = dtToExprt.Rows[1][i].ToString().Trim();
((Range)oSheet.Cells[2, i+1]).ColumnWidth = 20;
((Range)oSheet.Cells[2, i+1]).EntireColumn.WrapText = true;
((Range)oSheet.Cells[2, i + 1]).EntireRow.Font.Bold = 1;
((Range)oSheet.Cells[2, i + 1]).EntireRow.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
string strComments = dtToExprt.Rows[2][i].ToString().Trim();
((Range)oSheet.Cells[2, i + 1]).AddComment(strComments);
}
oWB.SaveCopyAs(strFilePath, Excel.XlFileFormat.xlWorkbookNormal, null, null, false,
false, Excel.XlSaveAsAccessMode.xlShared, false, false, null, null);
// Below line will kill the unkilled Excel process
Process[] pProcess;
pProcess = System.Diagnostics.Process.GetProcessesByName("EXCEL");
pProcess[0].Kill();
oSheet= null;
oWB = null;
oXL = null;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename="+cboTempName.SelectedItem.Text.ToString().Trim()+".xls");
Response.TransmitFile(strFilePath);
Response.End();
SIVARank #2,303 · 613 reputationPosted Jan 5, 2012, 4:00 AM EST
I got the answer for my problem..
i used File.Copy method for that as follows
string SourcePath = "D:\\Excel\\Emp.xls";
string Path="D:\\Excel\\";
string DestPath = Path +"Emp_"+ txtID.Text + ".xls";
System.IO.
File.Copy(SourcePath, DestPath);
Thank you,
SIVARank #2,303 · 613 reputationPosted Jan 5, 2012, 4:15 AM EST