1. 程式人生 > >采用OleDB讀取EXCEL文件 讀取數字後,字符串無法讀取

采用OleDB讀取EXCEL文件 讀取數字後,字符串無法讀取

tex i++ eem pty col ++ lena ons blog

  很多人采用OleDB讀取EXCEL文件的時候會發現,當一列數據以數字開頭的時候,後面的字符串無法讀取,今天就給大家分享一下解決此問題的小竅門。

  1、把列標題當做數據來讀取(HDR=NO設置把第一行當做數據而不是表頭來處理):

     string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" +
";Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\"";

  2、對讀取的數據進行處理:設置列名稱、刪掉第一行(列標題)

    private static void convertData(DataTable dt)
{
foreach (DataColumn dc in dt.Columns)
{

       //第一行其實應該是列名稱,所以直接拿來設置
string colName = dt.Rows[0][dc.ColumnName].ToString();
if (!string.IsNullOrEmpty(colName))
{
dc.ColumnName = getDataColumnName(dt, colName);
}
}
      //第一行任務完成,刪除它


dt.Rows.RemoveAt(0);
}

  完整代碼:

  

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace DepthDataConvert
{
    internal class ExcelFileReader
    {
        public static DataTable GetExcelData(string
fileName) { DataTable dt = new DataTable(); OleDbConnection conn = null; OleDbDataAdapter myCommand = null; try { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\""; conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; DataSet ds = null; strExcel = "select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); dt = ds.Tables[0]; } catch (Exception e) { throw e; } finally { if (conn.State == ConnectionState.Open) { conn.Close(); myCommand.Dispose(); conn.Dispose(); } } convertData(dt); removeEmpty(dt); return dt; } private static void removeEmpty(DataTable dt) { List<DataRow> removelist = new List<DataRow>(); for (int i = 0; i < dt.Rows.Count; i++) { bool IsNull = true; for (int j = 0; j < dt.Columns.Count; j++) { if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString().Trim())) { IsNull = false; } } if (IsNull) { removelist.Add(dt.Rows[i]); } } for (int i = 0; i < removelist.Count; i++) { dt.Rows.Remove(removelist[i]); } } private static void convertData(DataTable dt) { foreach (DataColumn dc in dt.Columns) { string colName = dt.Rows[0][dc.ColumnName].ToString(); if (!string.IsNullOrEmpty(colName)) { dc.ColumnName = getDataColumnName(dt, colName); } } dt.Rows.RemoveAt(0); } private static string getDataColumnName(DataTable dt, string cn) { string colName = cn; int index = 1; while (dt.Columns.Contains(colName)) { colName = cn + index++; } return colName; } public static void ExportExcel(DataTable dt) { if (dt == null || dt.Rows.Count == 0) return; Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { return; } System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; Microsoft.Office.Interop.Excel.Range range; long totalCount = dt.Rows.Count; long rowRead = 0; float percent = 0; for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; range.Interior.ColorIndex = 15; range.Font.Bold = true; } for (int r = 0; r < dt.Rows.Count; r++) { for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString(); } rowRead++; percent = ((float)(100 * rowRead)) / totalCount; } xlApp.Visible = true; } } }

采用OleDB讀取EXCEL文件 讀取數字後,字符串無法讀取