NewsFeed   -
KỸ THUẬT LẬP TRÌNH
S  M  L
Read Text, Excel files to DataTable - C#
-- Read text file to dataTable:
private DataTable ConvertFile(string DelimTextFile, string DelimCharacter)
    {
        DataTable dt = new DataTable();

        //Open the file in a stream reader.
        StreamReader s = new StreamReader(DelimTextFile);

        //Split the first line into the columns      
        string[] columns = s.ReadLine().Split(Delim.ToCharArray());

        foreach (string col in columns)
        {
            bool added = false;
            string next = "";
            int i = 0;

            while (!added)
            {
                //Build the column name and remove any unwanted characters.
                string columnname = col + next;
                columnname = columnname.Replace("#", "");
                columnname = columnname.Replace("'", "");
                columnname = columnname.Replace("&", "");

                //See if the column already exists
                if (!dt.Columns.Contains(columnname))
                {
                    dt.Columns.Add(columnname);
                    added = true;
                }
                else
                {
                    i++;
                    next = "_" + i.ToString();
                }
            }
        }
      
        //Read the rest of the data in the file.       
        string AllData = s.ReadToEnd();

        //Split off each row at the Carriage Return/Line Feed
        string[] rows = AllData.Split("\r\n".ToCharArray());

        //Now add each row to the DataTable
        foreach (string r in rows)
        {
            //Split the row at the delimiter.
            if (r != "")
            {
                string[] items = r.Split(Delim.ToCharArray());
                //Add the item
                dt.Rows.Add(items);
            }
        }
        return dt;
    }

-- EXCEL
private DataSet GetExcelWorkSheet(string FilePath, int workSheetNumber)
    {
        OleDbConnection ExcelConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
            + FilePath + ";Extended Properties=Excel 8.0;");
        OleDbCommand  ExcelCommand = new OleDbCommand();
        DataSet ds= new DataSet();
       
        ExcelCommand.Connection = ExcelConnection;
        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
        try
        {
            ExcelConnection.Open();
            DataTable ExcelSheets = ExcelConnection.GetOleDbSchemaTable(
                System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] {null,null,null,    "TABLE"}
            );
            string SpreadSheetName = "[" + ExcelSheets.Rows[workSheetNumber]["TABLE_NAME"].ToString() + "]";
            DataSet ExcelDataSet = new DataSet();
            ExcelCommand.CommandText = "SELECT * FROM " + SpreadSheetName.Replace("_", "");
            ExcelAdapter.Fill(ExcelDataSet);
            ExcelConnection.Close();
            ds = ExcelDataSet;
        }
        catch {}

        return ds;
    }

-- Convert Excel file to DataTable:
protected void ConvertFile(string FileToConvert)
    {
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
            FileToConvert + ";Extended Properties=Excel 8.0;";
        try
        {
            OleDbConnection connection = new OleDbConnection(connectionString);
            connection.Open();

            //Retrieve the correct sheets
            DataTable dt = new DataTable();
            dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            String[] excelSheets = new String[dt.Rows.Count];
            int i = 0;

            // Add the sheet name to the string array.
            ddListSheet.Items.Add(new ListItem("(Select One)","0"));
            foreach (DataRow row in dt.Rows)
            {
                excelSheets[i] = row["TABLE_NAME"].ToString();
                ddListSheet.Items.Add(new ListItem(row["TABLE_NAME"].ToString(), (i+1).ToString()));
                i++;
            }

            OleDbDataAdapter adapter;
            DataSet ds = new DataSet();
            // Loop through all of the sheets to put into dataset
            for (int j = 0; j < excelSheets.Length; j++)
            {
                adapter = new OleDbDataAdapter("SELECT * FROM [" + excelSheets[j] + "]", connection);
                dt = new DataTable();
                adapter.Fill(dt);
                ds.Tables.Add(dt);
            }

           catch (Exception ex) {}
}

- 06/17/2011
Search:
NGHỆ THUẬT - PHIM - ẢNH

HÌNH ẢNH ĐẸP

Photo

Album: CinemaGraphs
< July 2011 >
Sun Mon Tue Wed Thu Fri Sat
26 27 28 29 30 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6
More... (All Collections)
H2O NEWS

Everything Meaning To You!

Email: [email protected]

Website: emty.org

Liên hệ

Đặt làm trang chủ

@