2016-06-27 6 views
1

내 응용 프로그램에서 OpenOffice 스프레드 시트를 읽은 다음 그리드보기의 스프레드 시트에 데이터를 표시했는데 사용한 코드가 외부 테이블이 올바른 형식이 아닌 예외를 제공하고 있습니다 나는 here 설명으로이OpenOffice 스프레드 시트를 읽는 중 오류가 발생했습니다.

<div> 
     Import Excel File: 
     <asp:FileUpload ID="FileUpload1" runat="server" /> 
     <br /> 
     <br /> 
     <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /> 
     <br /> 
     <br /> 
     <asp:Label ID="Label1" runat="server"></asp:Label> 
     <br /> 
     <asp:GridView ID="gvExcelFile" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"> 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
      <EditRowStyle BackColor="#999999" /> 
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
      <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
      <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
      <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
     </asp:GridView> 

    </div> 

//Coneection String by default empty 
      string ConStr = ""; 
      //Extantion of the file upload control saving into ext because 
      //there are two types of extation .xls and .xlsx of Excel 
      string ext = Path.GetExtension(FileUpload1.FileName).ToLower(); 
      //getting the path of the file 
      string path = Server.MapPath("~/MyFolder/" + FileUpload1.FileName); 
      //saving the file inside the MyFolder of the server 
      FileUpload1.SaveAs(path); 
      Label1.Text = FileUpload1.FileName + "\'s Data showing into the GridView"; 
      //checking that extantion is .xls or .xlsx 
      if (ext.Trim() == ".ods") 
      { 
       //connection string for that file which extantion is .xls 
       ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; 
      } 
      else if (ext.Trim() == ".xlsx") 
      { 
       //connection string for that file which extantion is .xlsx 
       ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; 
      } 
      //making query 
      string query = "SELECT * FROM [Sheet1$]"; 
      //Providing connection 
      OleDbConnection conn = new OleDbConnection(ConStr); 
      //checking that connection state is closed or not if closed the 
      //open the connection 
      if (conn.State == ConnectionState.Closed) 
      { 
       conn.Open(); 
      } 
      //create command object 
      OleDbCommand cmd = new OleDbCommand(query, conn); 
      // create a data adapter and get the data into dataadapter 
      OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      //fill the Excel data to data set 
      da.Fill(ds); 
      //set data source of the grid view 
      gvExcelFile.DataSource = ds.Tables[0]; 
      //binding the gridview 
      gvExcelFile.DataBind(); 
      //close the connection 
      conn.Close(); 
+0

어떤 브라우저를 사용하고 있습니까? 또한 코드에서 "Excel"이라고 말하지만 대신 OpenOffice Calc를 사용해야한다고 생각합니다. 나는 질문을 편집했지만이 코드는 stackoverflow에서 눈살을 찌푸리기 때문에 코드를 변경하지 않았습니다. –

+0

나는 내 사무실 만 열었습니다. 사무실에 시스템이 설치되어 있습니다. 이제 사무실에서 스프레드 시트의 내용을 읽고 gridview에 표시해야합니다. 제게 많은 도움을 요청할 수는 있지만 할 수 없습니다. 매우 긴급한 요구 사항입니다. –

+0

음 , 그것이 긴급한 요구 사항이라는 사실은 stackoverflow와 관련이 없습니다 - 우리는 자원 봉사자입니다. 어쨌든 어떤 브라우저를 사용하고 있습니까? 또한 질문을 편집하고 "Excel"대신 "OpenOffice"또는 "Calc"라고 말하도록 코드를 변경하십시오. –

답변

0

ActiveXObject 만, Internet Explorer의 작동 해결할 수있는 방법에 대해 설명합니다.

다른 브라우저의 경우 실버 라이트는 AutomationFactory을 가질 수 있습니다. 그러나 보안 위험이 수반 되었기 때문에 시도하지 않았습니다.

또 다른 방법은 ReadExcel() 함수를 .js으로 끝나는 파일에 넣은 다음 파일 끝에 ReadExcel(); 줄을 추가하는 것입니다. excelFile을 스프레드 시트 파일의 경로로 설정하십시오. 그것을 실행하기 위해 더블 클릭 한 다음 OpenOffice 문서를 성공적으로로드했습니다.

편집 : 다음 줄이 같은 오류가 발생하는 경우

참조 : 그렇다면, 문제가 오픈 오피스 또는 코드에 관련되지 않은 경우

var objShell = new ActiveXObject("WScript.shell"); 

. 아마도 안전하지 못하게 실행할 수있는 권한을 Internet Explorer에 부여해야합니다. https://social.technet.microsoft.com/Forums/ie/en-US/8db7ec28-45ca-4859-b051-f0571a4da14e/error-automation-server-cant-create-object?forum=ieitpropriorver을 참조하십시오.

+0

효과가있는 경우 ms 사무실과 오픈 오피스가 동일한 PC에만 설치된 경우 작동하는 코드를 수정하십시오 오픈 오피스가 PC에 설치되면 오토메이션 서버가 객체를 생성 할 수 없다는 예외를 보여주고 있습니다 –

+0

오픈 오피스를 그리드 뷰에 펼친 시트 데이터를 표시하기 위해 긴급한 요구 사항을 도와주십시오 –

+0

또한 활성화 된 상태로 안전하게 실행할 수있는 권한을 부여했습니다 하지만 여전히 출력을 얻지 못하고있다 –

관련 문제