2014-07-23 1 views
0

oledb 리더를 사용하여 엑셀 시트를 읽는 데 어려움을 겪고 있습니다. 첫 번째 열은 독자가 돌아 오지 않고 마지막 열 머리 F14에 표시하고 열은 비어 있습니다. . 하지만 Excel 시트를 열고 열 머리글 테두리를 두 번 클릭하면 자동으로 크기가 조정되고 자동으로 크기가 저장되어 다시 완벽하게 반환되는 모든 열을 읽습니다.윈도우 응용 프로그램에서 oledb 리더를 읽지 않는 첫번째 열, 파일 데이터를 읽지 못함

PHP 응용 프로그램을 사용하여 생성하고 Excel에서 다운로드 한 Excel 시트는 엑셀에서 데이터를 읽는 데 응용할 수 있지만 위의 문제가 발생합니다.

나는 웹 응용 프로그램을 사용하여 Excel을 생성하는 동안 엑셀 시트에서 너비를 줄 수도있어 R & D를 이미 많이 만들었습니다. 내 코드는 우리가 첫 번째 셀을 고정의 나 쓰기 모드를 다운로드 할 때 우리는 첫 번째 열 및 행의 기본에서이 BCZ 헤더 장소를 IMEX = 3을 설정해야이

private bool Import_To_Grid(string FilePath, string Extension) 
     { 
      try 
      { 
       string conStr = ""; 
       switch (Extension) 
       { 
        case ".xls": //Excel 97-03 
         conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"] 
           .ConnectionString; 
         break; 
        case ".xlsx": //Excel 07 and above 
         conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"] 
            .ConnectionString; 
         break; 
       } 
       conStr = String.Format(conStr, FilePath); 
       OleDbConnection connExcel = new OleDbConnection(conStr); 
       OleDbCommand cmdExcel = new OleDbCommand(); 
       OleDbDataAdapter oda = new OleDbDataAdapter(); 

       cmdExcel.Connection = connExcel; 

       //Get the name of First Sheet 
       connExcel.Open(); 
       Exceldt = new DataTable(); 
       DataTable dtExcelSchema; 
       dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
       string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); 
       connExcel.Close(); 

       //Read Data from First Sheet 
       connExcel.Open(); 
       cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
       oda.SelectCommand = cmdExcel; 

       oda.Fill(Exceldt); 
       connExcel.Close(); 

       //Bind Data to GridView 
       dgv_showexcel.DataSource = Exceldt; 
       BindDataToCmbClass(); //binddata to class for filter 
       cmb_userclass.SelectedIndex = 0; 
       return true; 
      } 
      catch (Exception ex) { MessageBox.Show("Its Error" + " " + ex.ToString()); return false; } 
     } 


Connection string 
<add name="Excel03ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';" /> 
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';" /> 
+0

그것의 긴급 나는 이것에 대해 어떤 생각을 가지고 있다면 내가 당신을 감사와 함께 공유하시기 바랍니다주십시오 관한 일을하지 않았다 –

답변

0

같다.

0

방금 ​​같은 문제가 해결되었습니다. 어쩌면 누군가를 대신 할 수 있습니다.

필자의 경우 Excel 파일에는 열에 필터가 있습니다. 따라서 파일을 읽을 때 _xlnm#_FilterDatabase이라는 숨겨진 시트를 가져 오는 것이 좋습니다.

사실 나는 하나의 시트가 필요하므로 스키마 테이블의 첫 번째 행을보고 있습니다. 하지만 두 장이 하나 있는데 그 중 하나는 필터 시트이며이 시트에는 모든 열이 포함되어 있지 않습니다. 그래서 나는 첫 번째 칼럼을 얻지 못한다.

이 문제를 해결하려면 스키마를 순환하여 FilterDatabase이 포함되지 않은 첫 번째 시트 이름을 가져옵니다.

코드 회담 :

DataTable excelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
string sheetName = null; 
for (int i = 0; i < dtExcelSchema.Rows.Count; i++) 
{ 
    string tableNameOnRow = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString(); 
    if (!tableNameOnRow.Contains("FilterDatabase")) 
    { 
     sheetName = tableNameOnRow; 
     break; 
    } 
} 
OleDbDataAdapter da = new OleDbDataAdapter(); 
DataSet ds = new DataSet(); cmdExcel.CommandText = "SELECT * From [" + sheetName + "]"; 
관련 문제