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

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();

}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: