我需要在Windows窗体中打开视图和编辑Excel文件。
我尝试在Gridview中实现打开Excel文件的代码,然后对其进行编辑,但这会变得过长,因此我需要找到一种在WindowsForms中查看和编辑Excel文件的便捷方式。
任何帮助都将不胜感激。
谢谢,
答案 0 :(得分:0)
您可能希望使用 OLEDB 来读取和查询Excel,以下方法将excel文件中的特定工作表作为数据表返回,稍后您可以将其设置为gridview的数据源。 / p>
public static DataTable ConvertExcelToDataTableGroupSubGroup(string FileName)
{
DataTable dtResult = null;
int totalSheet = 0; //No of sheets on excel file
using (OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"))
{
objConn.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = string.Empty;
if (dt != null)
{
var tempDataTable = (from dataRow in dt.AsEnumerable()
where !dataRow["TABLE_NAME"].ToString().Contains("FilterDatabase")
select dataRow).CopyToDataTable();
dt = tempDataTable;
totalSheet = dt.Rows.Count;
// Sheet from first index
sheetName = dt.Rows[1]["TABLE_NAME"].ToString();
}
cmd.Connection = objConn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds, "excelData");
dtResult = ds.Tables["excelData"];
objConn.Close();
return dtResult; //Returning Dattable
}
}
OLEDB 还使您可以灵活地在Excel上编写 SQL查询。
答案 1 :(得分:0)
首先,您需要在NuGet中安装2个库
https://github.com/ExcelDataReader/ExcelDataReader
可以在没有Office或OLE连接的情况下读取和写入* .XLS,* .XLSX。
这是我正在使用的来源:)
using ExcelDataReader;
private DataSet ds;
IExcelDataReader reader = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
private void btnOpen_Click(object sender, EventArgs e)
{
openFileDialog.Filter = "Excel files (*.xls;*.xlsx)|*.xls;*.xlsx";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
tbPath.Text = openFileDialog.FileName;
var file = new FileInfo(tbPath.Text);
try
{
using (var stream = new FileStream(tbPath.Text, FileMode.Open))
{
if (reader != null) { reader = null; }
// Judge it is .xls or .xlsx
if (file.Extension == ".xls") { reader = ExcelReaderFactory.CreateBinaryReader(stream); }
else if (file.Extension == ".xlsx") { reader = ExcelReaderFactory.CreateOpenXmlReader(stream); }
if (reader == null) { return; }
ds = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = true,
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
UseHeaderRow = false,
ReadHeaderRow = (rowReader) => {
rowReader.Read();
},
// Gets or sets a callback to determine whether to include the
// current row in the DataTable.
FilterRow = (rowReader) => {
return true;
},
}
});
var tablenames = GetTablenames(ds.Tables);
cbSheet.DataSource = tablenames;
listSheet.DataSource = tablenames;
if (cbSheet.Items.Count == 1)
{
cbSheet.SelectedIndex = 0;
}
}
cbSheet.Enabled = true;
btnOpen.Enabled = true;
}
catch (Exception ex)
{
tbPath.Text = "";
cbSheet.Enabled = false;
btnOpen.Enabled = true;
MessageBox.Show(ex.Message);
}
}
}