C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Application windows, menus & toolbars »

How to create multiple worksheet in excel workbook using C#. net


Posted Date: 24 Sep 2009    Resource Type: Code Snippets    Category: Application windows, menus & toolbars
Author: Shanthi MMember Level: Gold    
Rating: 1 out of 5Points: 10



The following code is to create the multiple worksheets in the excel workbook using the C#.net. Before you code, add the references of Microsoft.Office.Interop.Excel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ex=Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Ex.Application exc = new Ex.Application();
Ex.Workbooks workbooks = exc.Workbooks;
Ex.Workbook workbook = exc.Workbooks.Add(Type.Missing);

Ex.Sheets sheets = exc.Sheets;
Ex._Worksheet worksheet = ((Ex._Worksheet)(sheets[1]));
Ex.Range range1;

worksheet.Name = "DeductorDetails";
Ex.Range xlCells;
range1 = worksheet.Cells;
xlCells = worksheet.Cells;

worksheet.Columns.ColumnWidth = 50;
worksheet.get_Range("A1", "A1").Font.Bold = true;
worksheet.get_Range("A1", "A1").Font.Size = 9;
worksheet.get_Range("A1", "A1").Font.Name = "Calibri";
xlCells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
xlCells[1, 1] = "TAX DEDUCTION ACCOUNT NO";

worksheet.get_Range("A2", "A2").Font.Bold = true;
worksheet.get_Range("A2", "A2").Font.Size = 9;
worksheet.get_Range("A2", "A2").Font.Name = "Calibri";
xlCells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
xlCells[2, 1] = "PERMANENT ACCOUNT NO";

worksheet.get_Range("A3", "A3").Font.Bold = true;
worksheet.get_Range("A3", "A3").Font.Size = 9;
worksheet.get_Range("A3", "A3").Font.Name = "Calibri";
xlCells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
xlCells[3, 1] = "FINANCIAL YEAR";


Ex.Range range2;
Ex.Range xlCells1;
range2 = worksheet.Cells;

xlCells1 = worksheet.Cells;
worksheet.get_Range("C1", "C1").Font.Bold = true;
worksheet.get_Range("C1", "C1").Font.Size = 9;
worksheet.get_Range("C1", "C1").Font.Name = "Calibri";
xlCells1.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlCells1[1, 3] = "SR NO";

Ex._Worksheet worksheet2 = ((Ex._Worksheet)(sheets[2]));
worksheet2.Name = "DEDUCTEE BREAK-UP";

Ex.Range range3;
range3 = worksheet2.Cells;


Ex.Range xlCells2;
range3 = worksheet2.Cells;

xlCells2 = worksheet2.Cells;
worksheet2.get_Range("D1", "D1").Font.Bold = true;
worksheet2.get_Range("D1", "D1").Font.Size = 9;
worksheet2.get_Range("D1", "D1").Font.Name = "Calibri";
xlCells2.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlCells2[1, 4] = "SR NO";

string sFile = @"C:\Documents and Settings\Shanthi_M01\Desktop\CustomData.xls";
workbook.SaveAs(sFile, Ex.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Ex.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

exc.Workbooks.Close();

}
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Multiple worksheet in excel workbook using C#. net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Multilanguage application development employing VS.NET
Previous Resource: Dynamic Search in Windows Application
Return to Discussion Resource Index
Post New Resource
Category: Application windows, menus & toolbars


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use