Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I get an exception while saving a excel into the location..

 

Source code :

 

private void CreateExcelWorkbook(string strsql) {

string strCurrentDir = Server.MapPath(".") + "\\";

RemoveFiles(strCurrentDir);

Excel.Application oXL;

Excel._Workbook oWB;

Excel._Worksheet oSheet;

Excel.Range oRng;

 

try {

GC.Collect();// clean up any other excel guys hangin' around...

oXL = new Excel.Application();

oXL.Visible = false;

//Get a new workbook.

oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));

oSheet = (Excel._Worksheet)oWB.ActiveSheet;

//get our Data

string strConnect = System.Configuration.ConfigurationManager.ConnectionStrings["SG"].ToString();

SPGen sg = new SPGen(strConnect, strsql);

SqlDataReader myReader = sg.RunReader();

// Create Header and sheet...

int iRow = 2;

for (int j = 0; j < myReader.FieldCount; j++) {

oSheet.Cells[1, j + 1] = myReader.GetName(j).ToString();

}

// build the sheet contents

while (myReader.Read()) {

for (int k = 0; k < myReader.FieldCount; k++) {

oSheet.Cells[iRow, k + 1] = myReader.GetValue(k).ToString();

}

iRow++;

}// end while

myReader.Close();

myReader = null;

//Format A1:Z1 as bold, vertical alignment = center.

oSheet.get_Range("A1", "Z1").Font.Bold = true;

oSheet.get_Range("A1", "Z1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;

//AutoFit columns A:Z.

oRng = oSheet.get_Range("A1", "Z1");

oRng.EntireColumn.AutoFit();

oRng = oSheet.get_Range("A1", "A1");

oRng.EntireColumn.Delete(Excel.XlDeleteShiftDirection.xlShiftToLeft);

oSheet.get_Range("A2", "A2").Select();

oXL.ActiveWindow.FreezePanes = true;

oXL.Visible = false;

oXL.UserControl = false;

//string strFile = "report" + System.DateTime.Now.Ticks.ToString() + ".xls";

string strFile = Utilities.CurrentUser.EIN.ToString().Replace(" ","_").ToString() + ".xls";

oWB.SaveAs(strCurrentDir + strFile, Excel.XlFileFormat.xlWorkbookNormal,

null, null, false, false, Excel.XlSaveAsAccessMode.xlShared, false, false, null, null, null);

// Need all following code to clean up and extingush all references!!!

oWB.Close(null, null, null);

oXL.Workbooks.Close();

oXL.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(oRng);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);

oSheet = null;

oWB = null;

oXL = null;

GC.Collect(); // force final cleanup!

string strMachineName = Request.ServerVariables["SERVER_NAME"];

errLabel.Text = "<A href=http://" + strMachineName + ":8080/Inventory/DM/" + strFile + ">Download Report</a>";

}

catch (System.Exception ex) {

String errorMessage;

errorMessage = "Error: ";

errorMessage = String.Concat(errorMessage, ex.Message);

errorMessage = String.Concat(errorMessage, " Line: ");

errorMessage = String.Concat(errorMessage, ex.Source);

errLabel.Text = errorMessage;

}

}

 

Can someone suggest whats the error..let me know if u need further info

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...