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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SIVAPosted Jan 5, 2012, 9:15 AM
SIVAPosted Jan 5, 2012, 9:00 AM
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,
AartiPosted Jan 5, 2012, 6:40 AM
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();