int i = 0;
Excel.Application xl = null;
Excel._Workbook wb = null;
Excel._Worksheet sheet = null;
object Missing = System.Reflection.Missing.Value;
//VBIDE.VBComponent module = null;
bool SaveChanges = false;
try
{
if (File.Exists(FileName)) { File.Delete(FileName); }
GC.Collect();
// Create a new instance of Excel from scratch
xl = new Excel.Application();
xl.Visible = false;
// Add one workbook to the instance of Excel
wb = (Excel._Workbook)(xl.Workbooks.Add(Missing));
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
// Get a reference to the one and only worksheet in our workbook
//sheet = (Excel._Worksheet)wb.ActiveSheet;
sheet = (Excel._Worksheet)(wb.Sheets[1]);
// Fill spreadsheet with sample data
sheet.Name = "Test";
SqlDataReader iDr = getFieldName("tbl_student_profile");
Excel.DropDowns xlDropDowns = null;
Excel.DropDown xlDropDown = null;
Excel.Range xlsRange = null;
xlsRange = sheet.get_Range("A1", "Z1");
xlsRange.Font.Bold = true;
xlsRange.ColumnWidth = 25;
xlsRange.RowHeight = 20;
xlsRange.EntireColumn.AutoFit();
int k = 0;
object objVal = null;
for (int n = 0; n <= iDr.FieldCount - 1; n++)
{
sheet.Cells[1, n + 1] = iDr.GetName(n);
}
for (k = 2; k <= 10; k++)
{
for (int n = 0; n <= iDr.FieldCount - 1; n++)
{
sheet.Cells[k, n + 1] = null;
if (iDr.GetName(n).ToString() == "city_ID")
{
xlsRange = sheet.get_Range("M" + k, Missing);
xlDropDowns = ((Excel.DropDowns)(sheet.DropDowns(Missing)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);
DataSet dsCity = getValue("tbl_city");
for (i = 0; i < dsCity.Tables[0].Rows.Count; i++)
{
objVal = dsCity.Tables[0].Rows[i][0];
xlDropDown.AddItem(dsCity.Tables[0].Rows[i][1].ToString(), objVal);
}
}
if (iDr.GetName(n).ToString() == "state_ID")
{
xlsRange = sheet.get_Range("N" + k, Missing);
xlDropDowns = ((Excel.DropDowns)(sheet.DropDowns(Missing)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);
DataSet dsState = getValue("tbl_state");
for (i = 0; i < dsState.Tables[0].Rows.Count; i++)
{
objVal = dsState.Tables[0].Rows[i][0];
xlDropDown.AddItem(dsState.Tables[0].Rows[i][1].ToString(), objVal);
}
}
if (iDr.GetName(n).ToString() == "country_ID")
{
xlsRange = sheet.get_Range("L" + k, Missing);
xlDropDowns = ((Excel.DropDowns)(sheet.DropDowns(Missing)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);
DataSet dsCountry = getValue("tbl_country");
for (i = 0; i < dsCountry.Tables[0].Rows.Count; i++)
{
objVal = dsCountry.Tables[0].Rows[i][0];
xlDropDown.AddItem(dsCountry.Tables[0].Rows[i][1].ToString(), objVal);
}
}
}
}
con.Close();
// Let loose control of the Excel instance
xl.Visible = false;
xl.UserControl = false;
// Set a flag saying that all is well and it is ok to save our changes to a file.
SaveChanges = true;
// Save the file to disk
wb.SaveAs(FileName, Excel.XlFileFormat.xlWorkbookNormal,
null, null, false, false, Excel.XlSaveAsAccessMode.xlShared,
false, false, null, null, null);
}
catch (Exception err)
{
msg = "Error: ";
msg = String.Concat(msg, err.Message);
msg = String.Concat(msg, " Line: ");
msg = String.Concat(msg, err.Source);
}
finally
{
try
{
// Repeat xl.Visible and xl.UserControl releases just to be sure
// we didn't error out ahead of time.
xl.Visible = false;
xl.UserControl = false;
// Close the document and avoid user prompts to save if our method failed.
wb.Close(SaveChanges, null, null);
xl.Workbooks.Close();
}
catch { }
// Gracefully exit out and destroy all COM objects to avoid hanging instances
// of Excel.exe whether our method failed or not.
xl.Quit();
if (File.Exists(FileName))
{
chkSave = "true";
}
//if (module != null) { Marshal.ReleaseComObject(module); }
if (sheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); }
if (wb != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); }
if (xl != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xl); }
//module = null;
sheet = null;
wb = null;
xl = null;
GC.Collect();
//con.Close();
}
private function()
{
int i = 0;
Excel.Application xl = null;
Excel._Workbook wb = null;
Excel._Worksheet sheet = null;
object Missing = System.Reflection.Missing.Value;
//VBIDE.VBComponent module = null;
bool SaveChanges = false;
try
{
if (File.Exists(FileName)) { File.Delete(FileName); }
GC.Collect();
// Create a new instance of Excel from scratch
xl = new Excel.Application();
xl.Visible = false;
// Add one workbook to the instance of Excel
wb = (Excel._Workbook)(xl.Workbooks.Add(Missing));
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
wb.Sheets.Add(Missing, Missing, Missing, Missing);
// Get a reference to the one and only worksheet in our workbook
//sheet = (Excel._Worksheet)wb.ActiveSheet;
sheet = (Excel._Worksheet)(wb.Sheets[1]);
// Fill spreadsheet with sample data
sheet.Name = "Test";
SqlDataReader iDr = getFieldName("tbl_student_profile");
Excel.DropDowns xlDropDowns = null;
Excel.DropDown xlDropDown = null;
Excel.Range xlsRange = null;
xlsRange = sheet.get_Range("A1", "Z1");
xlsRange.Font.Bold = true;
xlsRange.ColumnWidth = 25;
xlsRange.RowHeight = 20;
xlsRange.EntireColumn.AutoFit();
int k = 0;
object objVal = null;
for (int n = 0; n <= iDr.FieldCount - 1; n++)
{
sheet.Cells[1, n + 1] = iDr.GetName(n);
}
for (k = 2; k <= 10; k++)
{
for (int n = 0; n <= iDr.FieldCount - 1; n++)
{
sheet.Cells[k, n + 1] = null;
if (iDr.GetName(n).ToString() == "city_ID")
{
xlsRange = sheet.get_Range("M" + k, Missing);
xlDropDowns = ((Excel.DropDowns)(sheet.DropDowns(Missing)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);
DataSet dsCity = getValue("tbl_city");
for (i = 0; i < dsCity.Tables[0].Rows.Count; i++)
{
objVal = dsCity.Tables[0].Rows[i][0];
xlDropDown.AddItem(dsCity.Tables[0].Rows[i][1].ToString(), objVal);
}
}
if (iDr.GetName(n).ToString() == "state_ID")
{
xlsRange = sheet.get_Range("N" + k, Missing);
xlDropDowns = ((Excel.DropDowns)(sheet.DropDowns(Missing)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);
DataSet dsState = getValue("tbl_state");
for (i = 0; i < dsState.Tables[0].Rows.Count; i++)
{
objVal = dsState.Tables[0].Rows[i][0];
xlDropDown.AddItem(dsState.Tables[0].Rows[i][1].ToString(), objVal);
}
}
if (iDr.GetName(n).ToString() == "country_ID")
{
xlsRange = sheet.get_Range("L" + k, Missing);
xlDropDowns = ((Excel.DropDowns)(sheet.DropDowns(Missing)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);
DataSet dsCountry = getValue("tbl_country");
for (i = 0; i < dsCountry.Tables[0].Rows.Count; i++)
{
objVal = dsCountry.Tables[0].Rows[i][0];
xlDropDown.AddItem(dsCountry.Tables[0].Rows[i][1].ToString(), objVal);
}
}
}
}
con.Close();
// Let loose control of the Excel instance
xl.Visible = false;
xl.UserControl = false;
// Set a flag saying that all is well and it is ok to save our changes to a file.
SaveChanges = true;
// Save the file to disk
wb.SaveAs(FileName, Excel.XlFileFormat.xlWorkbookNormal,
null, null, false, false, Excel.XlSaveAsAccessMode.xlShared,
false, false, null, null, null);
}
catch (Exception err)
{
msg = "Error: ";
msg = String.Concat(msg, err.Message);
msg = String.Concat(msg, " Line: ");
msg = String.Concat(msg, err.Source);
}
finally
{
try
{
// Repeat xl.Visible and xl.UserControl releases just to be sure
// we didn't error out ahead of time.
xl.Visible = false;
xl.UserControl = false;
// Close the document and avoid user prompts to save if our method failed.
wb.Close(SaveChanges, null, null);
xl.Workbooks.Close();
}
catch { }
// Gracefully exit out and destroy all COM objects to avoid hanging instances
// of Excel.exe whether our method failed or not.
xl.Quit();
if (File.Exists(FileName))
{
chkSave = "true";
}
//if (module != null) { Marshal.ReleaseComObject(module); }
if (sheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); }
if (wb != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); }
if (xl != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xl); }
//module = null;
sheet = null;
wb = null;
xl = null;
GC.Collect();
//con.Close();
}