in my windows application i have to show excelwork
sheetwhich is stored in the local system can any one tell
me the code?



in my windows application i have to show excelwork sheetwhich is stored in the local system can an..

Answer / prashanthi

I'm giving you the code about export to excel i.e., now i'm
taking table from database and binding to excel sheet.That
Excel sheel is on my Local System.
Here i'm using Aspose(its a 3rd Party Tool)
Aspose reduces the code by setting data ,cells etc in Excel
Sheet.
Its a WebApplication.U can change it to Windows Application.

This code is for Setting the format for Excel Sheet:
public Workbook ExportToExcel(DataSet objDs, string
strHeader)
{
DataTable dtResult;
dtResult = objDs.Tables[0];

Workbook excel = new Workbook();
excel.Worksheets[0].Name = strHeader;

#region SettingHeaderInformation

excel.Worksheets[0].Cells
["B4"].Style.Font.IsBold = true;
excel.Worksheets[0].Cells["B4"].Style.Font.Size
= 12;
excel.Worksheets[0].Cells["B4"].PutValue
(strHeader);

excel.Worksheets[0].Cells
["D4"].Style.Font.IsItalic = true;
excel.Worksheets[0].Cells["D4"].PutValue
("Dated: " + DateTime.Now.ToLongDateString());

#endregion

//Exports data set into excel cells
Aspose.Cells.Cells exportCells =
excel.Worksheets[0].Cells;
exportCells.ImportDataTable(dtResult, false, 5,
0);

//This block is to header for exported data
table.
Aspose.Cells.Cells sheetCell = excel.Worksheets
[0].Cells;
for (int i = 0; i < dtResult.Columns.Count; i++)
{
if (i != (objDs.Tables[0].Columns.Count -
1))
excel.Worksheets[0].AutoFitColumn(i);

excel.Worksheets[0].Cells[5,
i].Style.Number = 49;
excel.Worksheets[0].Cells[5,
i].Style.Pattern = BackgroundType.Solid;
excel.Worksheets[0].Cells[5,
i].Style.ForegroundColor = excel.GetMatchingColor
(Color.FromName(SystemColors.InactiveCaption.Name));
excel.Worksheets[0].Cells[5,
i].Style.Font.IsBold = true;
excel.Worksheets[0].Cells[5,
i].Style.Font.Color = Color.White;
string headerTxt = dtResult.Columns
[i].ColumnName;
if (headerTxt.Contains(strDesc))
headerTxt = headerTxt.Remove
(headerTxt.IndexOf(strDesc));
else if (headerTxt.Contains(strDesc))
headerTxt = headerTxt.Remove
(headerTxt.IndexOf(strDesc));
excel.Worksheets[0].Cells[5, i].PutValue
(headerTxt);
if (i < objDs.Tables[0].Columns.Count)
excel.Worksheets[0].Cells[5,
i].Style.HorizontalAlignment = TextAlignmentType.Left;
else
excel.Worksheets[0].Cells[5,
i].Style.HorizontalAlignment = TextAlignmentType.Right;
}

return excel;
}


In Button we can write this code(its for binding the data
from database to Excel Sheet):

protected void Button2_Click(object sender, EventArgs e)
{
Aspose.Cells.Workbook workBook = new
Aspose.Cells.Workbook();
ExportMethods objExpMethods = new ExportMethods();
ds = objclass.getpagebal();//calling function from
businesslayer(table)
workBook = objExpMethods.ExportToExcel
(ds, "shanti_emp");//shanti_emp is a table name
workBook.Save("shantiExcel.xls",
FileFormatType.Default,
Aspose.Cells.SaveType.OpenInBrowser, Response);
}

U can convert it according to your convineince.

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More VB.NET Interview Questions

Explain how does the xmlserializer work?

0 Answers  


How can I extract the formated word(bold,italic,underline,font,color etc) from the msword file.?

0 Answers  


thak you Mr Govind for replying to my question. My next question is that how to retrieve image stored in an SQL server table and assign it to any image control or picture control using VB.net

0 Answers  


Hi I am planning to take interview in VB.net,can any one share your interview Questions for a Entry-level job. Regards Lina

3 Answers  


What is VB.Net?

3 Answers   Ksb,






How to connect crystal report in vb.net ?

0 Answers  


I want to reduce my CPU Usage when I am querying the Database for records. Now my Winform App(in VB.Net) suddenly Jumps to 100% CPU Usage when i query for records from sql server database. I want this Usage to be less. Any help is appreciated

2 Answers  


What is the use of system.diagnostics.process class?

0 Answers  


I have an opening for "APRIMO" Developer in Mumbai Location

1 Answers  


Disadvantages of vb ?

12 Answers   Digital GlobalSoft, TCS,


Explain how to achieve polymorphism in vb.net?

0 Answers  


In order to get assembly info whcih namespace we should import?

1 Answers  


Categories