내 응용 프로그램에서 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();
어떤 브라우저를 사용하고 있습니까? 또한 코드에서 "Excel"이라고 말하지만 대신 OpenOffice Calc를 사용해야한다고 생각합니다. 나는 질문을 편집했지만이 코드는 stackoverflow에서 눈살을 찌푸리기 때문에 코드를 변경하지 않았습니다. –
나는 내 사무실 만 열었습니다. 사무실에 시스템이 설치되어 있습니다. 이제 사무실에서 스프레드 시트의 내용을 읽고 gridview에 표시해야합니다. 제게 많은 도움을 요청할 수는 있지만 할 수 없습니다. 매우 긴급한 요구 사항입니다. –
음 , 그것이 긴급한 요구 사항이라는 사실은 stackoverflow와 관련이 없습니다 - 우리는 자원 봉사자입니다. 어쨌든 어떤 브라우저를 사용하고 있습니까? 또한 질문을 편집하고 "Excel"대신 "OpenOffice"또는 "Calc"라고 말하도록 코드를 변경하십시오. –