Monday, December 29, 2008

Execl to Folder ceation in file handling

Hi Gagan,
In this post I will explain how to create folder if we we have already name of folder in our Excel file.
Here I have Excel

I need to create folder as mentioned in its first column

In C#.net we need to create Oledb connectioon to access data from Excel.
I have given Sample code you can understand easily by it

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

using System.IO;

namespace xlsToFolder

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"D:\c3.xls" + ";Extended Properties=Excel 8.0");

con.Open();

try

{

DataSet myDataSet = new DataSet();

OleDbDataAdapter myCommand = new OleDbDataAdapter(" SELECT * FROM [" + "Sheet1" + "$]", con);

myCommand.Fill(myDataSet);

con.Close();

//MessageBox.Show(myCommand.ToString());

// textBox1.AppendText("\nDataSet Filled");

//Travers through each row in the dataset

foreach (DataRow myDataRow in myDataSet.Tables[0].Rows)

{

//Stores info in Datarow into an array

Object[] cells = myDataRow.ItemArray;

//Traverse through each array and put into object cellContent as type Object

//Using Object as for some reason the Dataset reads some blank value which

//causes a hissy fit when trying to read. By using object I can convert to

//String at a later point.

foreach (object cellContent in cells)

{

//Convert object cellContect into String to read whilst replacing Line Breaks with a defined character

string cellText = cellContent.ToString();


//It will create the folder under folder named Test4 System.IO.Directory.CreateDirectory("c:/Test4/" + cellText);

}

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

con.Close();

}

}

}

}






No comments:

Post a Comment