using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Web;
//using System.Web.UI;
//using System.Web.UI.WebControls;
using System.Configuration;


namespace QuanLiDSSV
{
    public partial class Frm2 : Form
    {
        public Frm2()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
           

        }
        private static string OpenExcelFile(string fPath)
        {
            string connectionstring = String.Empty;
            string[] splitdot = fPath.Split(new char[1] { '.' });
            string dot = splitdot[splitdot.Length - 1].ToLower();
            if (dot == "xls")
            {
                //tao chuoi ket noi voi Excel 2003
                connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
            }
            else if (dot == "xlsx")
            {
                //tao chuoi ket noi voi Excel 2007
                connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fPath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            }
            return connectionstring;
        }
        /// <summary>
        /// Uploads the file.
        /// </summary>
        /// <param name="fUpload">The upload control .</param>
        /// <param name="fPath">The path string.</param>
        public static string UploadFile(FileUpload fUpload)
        {
            FileUpload fUpload;
            string fPath = String.Empty;
            try
            {
                fPath = HttpContext.Current.Server.MapPath("~/upload/temp/" + fUpload.DSSV);
                FileInfo fInfo = new FileInfo(fPath);
                if (fInfo.Exists)
                {
                    fInfo.Delete();
                }
                fUpload.SaveAs(fPath);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return fPath;
        }
        /// <summary>
        /// Gets the name of the sheet.
        /// </summary>
        /// <param name="fPath">The f path.</param>
        /// <returns>return all sheet name</returns>
        public static ArrayList GetSheetName(string fPath)
        {
            ArrayList sheetnames = new ArrayList();
            string connectionstring = OpenExcelFile(fPath);
            //mo ket noi den file excel
            OleDbConnection cnn = new OleDbConnection(connectionstring);
            cnn.Open();

            //tao bang luu tru tam cac du lieu trong file 
            DataTable table = new DataTable();
            table = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            //doc tung dong trong bang luu tru tam
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string name = table.Rows[i][2].ToString().Replace("'", "");//get ten tung sheet co trong bang luu tru
                //kiem tra sheet
                if (name.EndsWith("$"))
                {
                    sheetnames.Add(name.Replace("$", ""));
                }
            }
            cnn.Close();
            table.Dispose();
            return sheetnames;
        }
        /// <summary>
        /// Gets the data excel.
        /// </summary>
        /// <param name="fPath">The f path.</param>
        /// <param name="sheetname">The sheetname.</param>
        /// <returns>return dataset</returns>

    }
}

i want to connect excel file and show it's path on textbox!
Please help me!
Thank u so much!

Recommended Answers

All 6 Replies

What part of your code do you need help with?
You have methods to upload, read and extract sheet names, but you arent calling any of them.
If you want to output the sheet names to a textbox you need to input a filename somehow (either type it into a textbox or use an OpenFileDialog control) then call the GetSheetNames method to retrieve an ArrayList of all the sheets in the file. Once you have the arraylsit you can output each string into your textbox.

i made it but appear error :The type or namespace name 'FileUpload' could not be found (are you missing a using directive or an assembly reference?)

Please help me
Thank u so much!!

You have commented out the WebUI and WebControls references at the top of the code. FileUpload is contained in the System.Web.UI.WebControls namespace.

If i insert "using system.web.UI and using system.web.UI.webcontrols ". It show some error:

Warning 1. The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:
LoadExcel --- The base class 'System.Object' cannot be designed.
Frm2 --- The base class 'System.Object' cannot be designed. 0 0

Error 2
The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) E:\luan van\bt\QuanLiDSSV\QuanLiDSSV\Form2.cs 8 18 QuanLiDSSV

Error 3 The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) E:\luan van\bt\QuanLiDSSV\QuanLiDSSV\Form2.cs 9 18 QuanLiDSSV

Error 4 The type or namespace name 'FileUpload' could not be found (are you missing a using directive or an assembly reference?) E:\luan van\bt\QuanLiDSSV\QuanLiDSSV\Form2.cs 40 41 QuanLiDSSV

The code you are trying to use is for a web application (ASP.NET) if this is a windows based application then you have sourced the wrong code.
If you are jsut trying to open and read the file then you can remove the entire UploadFile method.

i want to read excel file's content and then add all into one table in access!
Please help me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.