C#. Net ile excel veri satırını nasıl okuyabileceğimi bilen var mı?excel veri satırını C# .net ile okuyabilirsiniz.
Verileri excel'den döndürecek ve C# .net öğesindeki grindview'da görüntülenecek bu kodu buldum. Her ne kadar, sadece geziyorum, bunun yerine sunucu tarafındaki veri hattını okumak mümkün olacak mı?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace site
{
public partial class pgTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnImport_Click(object sender, EventArgs e)
{
string connString = "";
string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
string path = fileuploadExcel.PostedFile.FileName;
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT [username],[age],[phone] FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
da.Dispose();
conn.Close();
conn.Dispose();
}
}
}
aşağıdaki gibi
OleDbDataReader
kullanabilirsiniz; Verileri bir "grvExcelData" (sadece Grid görünümü ile bir şey yapmak zorunda sanırım) içine bağlama. Veya daha da iyisi; Microsoft.Office.Interop.Excel API'lerini – aquaragakullanabilirsiniz, bu kod veri kümesinde ne istediğinizi sağlar, değil mi? –
Sadece excel dosyaları satır satır okumak ve tüm excel dosyasını okuma ve değerini ızgaraya yükleme değil kontrol etmek istediğini düşünüyorum. –