Click here to Skip to main content
14,639,719 members
Rate this:
Please Sign up or sign in to vote.
See more:
code for read all excel sheets from the system and send that excels to folder using c#.net. urgently i want. please help me.
Posted
Comments
[no name] 25-Jan-13 1:31am
   
please be elobarate your question...

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

As per my understand your question is looks like upload the excel file and display the sheets in that excel and then that excel to be stored in particular folder...
right...?

but directly you ask coding this is not a correct way . If your asking coding some one given the code..
But you can't understand the logic of the code...
First of all you try by your self , any error came you search by your self then also your doubt is not clear then only you post the questions on this site ...

this is the source code
<table>
   <tr>
      <td align="center" colspan="3">
           <asp:label id="Label1" runat="server" text="Drawing Schedule" cssclass="PageHead" xmlns:asp="#unknown"></asp:label>
       </td>
    </tr>
    <tr>
       <td align="right">
                          Excel File :
       </td>
       <td align="left">
         <asp:fileupload id="FileUpload1" width="50%" runat="server" xmlns:asp="#unknown" />
         <asp:label id="lblmsg" runat="server" text="" xmlns:asp="#unknown"></asp:label>
          
         <asp:button id="btnGetExcelSheetList" runat="server" onclick="btnGetExcelSheetList_Click" xmlns:asp="#unknown">
                      Text="Get Excel Sheet List" />
                    
              <br />
        </asp:button></td>

      </tr>
      <tr>
          <td align="right" valign="top" colspan="" class="style1">
                  <asp:label id="lblsheet" runat="server" text="Sheet Name : " xmlns:asp="#unknown"></asp:label>
          </td>
          <td valign="top" colspan="" class="style6">
                  <asp:dropdownlist id="ddlSheet" runat="server" autopostback="True" visible="False" xmlns:asp="#unknown">
                   SkinID="DropDownListSkin"
                      OnSelectedIndexChanged="ddlSheet_SelectedIndexChanged">
                  </asp:dropdownlist>
                  <br />
               <br />
          </td>
       </tr>
  </table>



In your CodeBehind:

private void Get_Sheets()
{
    OleDbConnection oconn = null;
    DataTable dt = null;
    try
    {
        string FilePath = string.Empty;
        string FileName = string.Empty;
        if (FileUpload1.HasFile)
        {
            FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            string FolderPath = ConfigurationManager.AppSettings["PRPOFolder"];
            FilePath = Server.MapPath(FolderPath + FileName);
            ViewState["FilePath"] = FilePath;
            ViewState["FileName"] = FileName;
            FileUpload1.SaveAs(FilePath);
        }
        //Microsoft Office 12.0 Access Database Engine OLE DB Provider
        oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0");

        oconn.Open();
        dt = null;
        dt = oconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        if (dt == null)
        {

        }
        String[] sheet = new String[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            sheet[i] = dr["TABLE_NAME"].ToString();
            i++;
        }
        string[] a = sheet;
        int j = 0;
        if (a != null && a.Length > 0)
        {
            ddlSheet.Visible = true;
            lblsheet.Visible = true;
            for (j = 0; j < a.Length; j++)
            {
                ddlSheet.Items.Add(a[j]);
            }
            ddlSheet.Items.Insert(0, "<--- Select Excel Sheet --->");
        }
        else
        {
            ddlSheet.Visible = false;
            lblsheet.Visible = false;
        }
    }
    catch (Exception ex)
    {
    }
    finally
    {
        if (oconn != null)
        {
            oconn.Close();
            oconn.Dispose();
        }
        if (dt != null)
        {
            dt.Dispose();
        }
    }
}
protected void btnGetExcelSheetList_Click(object sender, EventArgs e)
{
    ddlSheet.Items.Clear();
    Get_Sheets();
}
   
v3
Comments
kavithakumari 25-Jan-13 2:18am
   
hi,
actually i want read all excel file name with extensions from the system and that names saved in to the folder .

i am writing some code i.e
private void button1_Click(object sender, EventArgs e)
{

foreach (string file in GetFiles("C:\\"))
{
// Console.WriteLine(file);
listBox2.Items.Add(file);
}

}


static IEnumerable<string> GetFiles(string path)
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0)
{
path = queue.Dequeue();
try
{
foreach (string subDir in Directory.GetDirectories(path))
{
queue.Enqueue(subDir);
}
}
catch (Exception ex)
{
MessageBox.Show("ex");
Console.Error.WriteLine(ex);
}
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
}
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
yield return files[i];

}
}
}
}

but this code shows one error .error is

Access to the path 'C:\Documents and Settings' is denied.

how to rectify this error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100