using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Exl = Microsoft.Office.Interop.Excel;
public class createExcel
{
//Excel Objects
Exl.Application excel;
Exl.Workbook workBook;
Exl.Workbooks workBooks;
Exl.Sheets sheets;
Exl._Worksheet workSheet;
Exl.Range range;
Exl.Range rangeCells;
public createExcel()
{
InitializeExcelObjects();
}
public void InitializeExcelObjects()
{
excel = new Exl.Application();
workBooks = excel.Workbooks;
workBook = excel.Workbooks.Add(Type.Missing);
//sheets = excel.Sheets;
//wSheet = (Exl._Worksheet)(sheets[1]);
}
private int ExcelTemplate(string fileName, DataTable dtData)
{
int nmReturn=0, nmSheetIndex=1, nmColumnCount=1;
StringBuilder strBuild = new StringBuilder();
string sheetName = string.Empty;
try
{
sheetName = dtData.Rows[0][1].ToString();
sheets = excel.Sheets;
workSheet = ((Exl.Worksheet)sheets[nmSheetIndex]);
workSheet.Name = sheetName;
range = workSheet.Cells;
rangeCells = workSheet.Cells;
for (int rowCount = 0; rowCount < dtData.Rows.Count; rowCount++)
{
if (!sheetName.Equals(dtData.Rows[rowCount][1].ToString()))
{
nmSheetIndex++;
sheetName = dtData.Rows[rowCount][1].ToString();
workSheet = ((Exl.Worksheet)sheets[nmSheetIndex]);
workSheet.Name = sheetName;
range = workSheet.Cells;
rangeCells = workSheet.Cells;
nmColumnCount = 1;
}
//workSheet.Columns.ColumnWidth = 50;
rangeCells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
rangeCells[1, nmColumnCount] = dtData.Rows[rowCount][0].ToString();
nmColumnCount++;
}
string filePath = Application.StartupPath.ToString() + fileName;
workBook.SaveAs(filePath, Exl.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Exl.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel.Workbooks.Close();
nmReturn++;
}
catch (Exception ex) { nmReturn = 0; }
return nmReturn;
}
}
Subscribe to:
Post Comments (Atom)
Motivational qoutes
पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।
-
H ere is the code example given to add Image column in Datagrid control dynamically at run time in WPF. public void AddColumnsToDataGrid(...
-
S ome times we need to add some controls in a container at run time. Here is the coding logic given to add CheckBox column in Datagrid cont...
-
H ow to add TreeViewItem dynamically in TreeView control in WPF. Here is the coding example given below to add TreeViewItem in TreeView con...
No comments:
Post a Comment
Put your comments here