PDA

View Full Version : سوال: چگونه خالی بودن سطر در اکسل را چک کنیم



sohrabi1383
جمعه 08 دی 1396, 17:55 عصر
بنده برای خواندن فایل اکسل از کد زیر استفاده کرده ام

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


namespace khandanexcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
var fileName = @"|DataDirectory|\1.xlsx";
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties="Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedType s=Text"";
var conn = new OleDbConnection(connectionString);
conn.Open();
// var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSc hemaGuid.Tables, new object[] { null, null, null, "person" });
var cmd = conn.CreateCommand();
// cmd.CommandText = "SELECT * FROM [" + sheets.Rows[1]["A"].ToString() + "] ";
cmd.CommandText = "SELECT * FROM [Sheet1$]";
var adapter = new OleDbDataAdapter(cmd);
var ds = new DataSet();
DataTable dt = new DataTable();
adapter.Fill(ds);
adapter.Fill(dt);
textBox1.Text = dt.Rows[0][0].ToString();
textBox2.Text = dt.Rows[0][1].ToString();
textBox3.Text = dt.Rows[0][2].ToString();
textBox4.Text = dt.Rows[1][0].ToString();//1 sotun 0 satr
textBox5.Text = dt.Rows[1][1].ToString();
textBox6.Text = dt.Rows[1][2].ToString();
textBox7.Text = dt.Rows[2][0].ToString();
textBox8.Text = dt.Rows[2][1].ToString();
textBox9.Text = dt.Rows[2][2].ToString();
textBox10.Text = dt.Rows[3][0].ToString();
textBox11.Text = dt.Rows[3][1].ToString();
textBox12.Text = dt.Rows[3][2].ToString();



conn.Close();

}
}
}



ولی اگر یکی از سطرها خالی باشد برنامه خطا می دهد چگونه می توانم قبلا از خواندن سطر خالی بودن آن را چک کنم

Mahmoud.Afrad
جمعه 08 دی 1396, 23:37 عصر
متن خطا رو چرا قرار نمیدید؟

به جای ToString از Convert.ToString (https://msdn.microsoft.com/en-us/library/system.convert.tostring.aspx) استفاده کنید.