我试图在消息框中显示excel表名称,但似乎它不起作用,有人可以帮助我。
问题描述:
我是编程新手。我试图在消息框中显示excel表名,但似乎它不起作用,有人可以帮助我。
我尝试了什么:
我试过这个编码但是没有显示消息框。我是否需要在某处调用此函数,例如button1_Click?说真的,我不知道该怎么做。感谢您帮助我
I'm new to the programming. I am trying to show excel sheet name in Message box but it seems like its not working , can somebody please help me.
What I have tried:
I've tried this coding but the message box is not showing. Do i need to call this function somewhere such as in button1_Click? Seriously i dont know what i should do. Thank You for helping me
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.IO;
using System.Data.OleDb;
namespace Filter2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private String[] GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
// Connection String. Change the excel file to the file you
// will search.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + "SamsungGT-i9300GalaxySIII- demo (pendrive).xlsx" + ";Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection string.
objConn = new OleDbConnection(connString);
// Open connection with the database.
objConn.Open();
// Get the data table containg the schema guid.
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
// Loop through all of the sheets if you want too...
for (int j = 0; j < excelSheets.Length; j++)
{
// Query each excel sheet.
}
return excelSheets;
}
catch (Exception ex)
{
return null;
}
finally
{
// Clean up.
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
}
}
答
获取摆脱尝试捕获。你正在吞咽任何抛出的异常,这是非常非常糟糕的做法。如果您使用使用
你也需要处理等等你需要正确调试 - 这里是如果你不确定如何做到这一篇文章:掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]
试试这个
Get rid of the try-catch. You are "swallowing" any exceptions that are thrown, which is very, very bad practice. You also don't need dispose etc if you useusing
You need to debug this properly - here is a starter article if you are not sure how to do that: Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Try this
private String[] GetExcelSheetNames(string excelFile)
{
var connString = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
var excelSheets = new string[0];
using(var objConn = new OleDbConnection(connString))
{
objConn.Open();
// Get the data table containg the schema guid.
using (var dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
{
if (dt != null)
{
excelSheets = new String[dt.Rows.Count];
for (var i = 0; i < dt.Rows.Count; i++)
excelSheets[i] = dt.Rows[i]["TABLE_NAME"].ToString();
}
objConn.Close();
}
}
foreach (var sht in excelSheets)
Debug.Print(sht);
return excelSheets;
}
是的,你需要从某处调用它,按钮点击事件是个不错的选择。
Yes, you will need to call this from somewhere, and a button click event is a good choice.