PDA

View Full Version : مشکل در ارسال اکسل 2007 به بالا به datagridview



hamidtmu
جمعه 09 فروردین 1398, 10:12 صبح
با سلام و تبریک عید نوروز به برنامه نویسان عزیز
من این کد را نوشتم که در ارسال اطلاعات اکسل xls به دیتا گرید ویوهیچ مشکلی نداره ولی در ارسال اطلاعات xlsxبه دیتا گرید ویو مشکل داره و اطلاعات با نمایش نمی ده-سایت های مختلفی حتی همین برنامه نویس را گشتم ولی نتونستم مشکلمو حل کنم. ممنون میشم یه بزرگوار کارشناسی کنه کد را و مشکل را بهم بگه/ لازم به ذکره که تا کنون سه چهار تا پروژه آماده هم خریدم جواب نمیده/خواهشا کمک کنید

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

using System.IO;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace strong
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
//Form6 f6 = new Form6();

SqlConnection con = new SqlConnection(@"server = .\SQLEXPRESS;database = strongDB;trusted_connection=yes;");

public DataTable ReadExcel(string fileName, string fileExt)
{
string conn = string.Empty;
DataTable dtexcel = new DataTable();
if (fileExt.CompareTo(".xls") == 0)

conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //for below excel 2007
else
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' fileName '; Extended Properties='Excel 12.0 XML;HDR=YES';"; //for above excel 2007
using (OleDbConnection con = new OleDbConnection(conn))
{
try
{
OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con); //here we read data from sheet1
oleAdpt.Fill(dtexcel); //fill excel data into dataTable
}
catch { }
}
return dtexcel;
}



private void button5_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user
{
filePath = file.FileName; //get the path of the file
fileExt = Path.GetExtension(filePath); //get the file extension
if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
{
try
{
DataTable dtExcel = new DataTable();
dtExcel = ReadExcel(filePath, fileExt); //read excel file
dataGridView1.Visible = true;
dataGridView1.DataSource = dtExcel;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
{
MessageBox.Show("Please choose .xls or .xlsx file only.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error
}
}
}

saeidfar
جمعه 23 فروردین 1398, 00:11 صبح
معمولا مشکل از Microsoft.Office.Interop.Excel

هست. (توی رفرنسهای پروژه)
باید نسخه 15 رو نصب(تغییر بدین) کنید که بتونه با آفیس های بالاتر کارکنه
احتمالا ورژن نسخه شما 12 هست.

Mahmoud.Afrad
جمعه 23 فروردین 1398, 00:41 صبح
در متد ReadExcel خط 39 و 44 را حذف کنید ببینید پیغام خطا چیه.

hamidtmu
شنبه 24 فروردین 1398, 00:07 صبح
با تشکر . فکر می کنم مشکل اینه.باید تست کنم دوباره(روشمو عوض کرده بودم چون جوابی براش پیدا نکرده بودم)