C#窗体程序,谁能帮忙一下给一个com组件操作读写Excel文件,显示在gridview控件中。
我看别人写的怎么会报错?谁能帮帮忙呀
namespace OpenExcelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet ds;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfileDialog = new OpenFileDialog(); if (openfileDialog.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = openfileDialog.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
string filepath = this.textBox1.Text; dataGridView1.DataSource = ToDataTableEx(filepath);
ds.Merge(ToDataTableEx(filepath));
}
private void button3_Click(object sender, EventArgs e)
{ System.Data.DataTable dt = new System.Data.DataTable();
string file = this.textBox2.Text; DataTabletoExcel(dt, file); } /// <summary>
/// 用Excel Com组件方式读取Excel内容到DataSet
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static DataSet ToDataTableEx(string path)
{
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Workbooks.Add(path); DataSet ds = new DataSet();
try
{
//遍历Worksheets中的每张表
for (int i = 1; i <= excel.Worksheets.Count; i++)
{
//获得指定表
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Worksheets[i];
System.Data.DataTable dt = new System.Data.DataTable(); //取表明赋值到dt TableName
dt.TableName = worksheet.Name; int r; //Row坐标
int c; //Column坐标
int intRows; //行数
int intCols; //列数 //取数据
Microsoft.Office.Interop.Excel.Range excelCell = worksheet.UsedRange;
Object[,] values = (Object[,])excelCell.Value2;
intRows = values.GetLength(0);
if (intRows != 0)
{
intCols = values.GetLength(1);
if (intCols != 0)
{
//遍历column第一行 得到字段
for (c = 1; c <= intCols; c++)
{
dt.Columns.Add(new DataColumn((String)values[1, c]));
} //赋值
for (r = 2; r <= intRows; r++)
{
DataRow dr = dt.NewRow();
for (c = 1; c <= intCols; c++)
{
dr[(String)values[1, c]] = values[r, c];
}
dt.Rows.Add(dr);
}
}
}
ds.Tables.Add(dt);
}
}
catch (Exception ex)
{
throw (ex);
}
finally
{
excel.Quit();
int generation = System.GC.GetGeneration(excel);
System.GC.Collect(generation);
}
return ds;
}
public void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = false;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true); //将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
} //将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
//xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8));
xlBook.SaveCopyAs(strFileName); xlApp.Workbooks.Close();
xlApp.Quit();
int generation = System.GC.GetGeneration(xlApp);
System.GC.Collect(generation);
}
}
}C# excel com lib dll
我看别人写的怎么会报错?谁能帮帮忙呀
namespace OpenExcelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet ds;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfileDialog = new OpenFileDialog(); if (openfileDialog.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = openfileDialog.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
string filepath = this.textBox1.Text; dataGridView1.DataSource = ToDataTableEx(filepath);
ds.Merge(ToDataTableEx(filepath));
}
private void button3_Click(object sender, EventArgs e)
{ System.Data.DataTable dt = new System.Data.DataTable();
string file = this.textBox2.Text; DataTabletoExcel(dt, file); } /// <summary>
/// 用Excel Com组件方式读取Excel内容到DataSet
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static DataSet ToDataTableEx(string path)
{
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Workbooks.Add(path); DataSet ds = new DataSet();
try
{
//遍历Worksheets中的每张表
for (int i = 1; i <= excel.Worksheets.Count; i++)
{
//获得指定表
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Worksheets[i];
System.Data.DataTable dt = new System.Data.DataTable(); //取表明赋值到dt TableName
dt.TableName = worksheet.Name; int r; //Row坐标
int c; //Column坐标
int intRows; //行数
int intCols; //列数 //取数据
Microsoft.Office.Interop.Excel.Range excelCell = worksheet.UsedRange;
Object[,] values = (Object[,])excelCell.Value2;
intRows = values.GetLength(0);
if (intRows != 0)
{
intCols = values.GetLength(1);
if (intCols != 0)
{
//遍历column第一行 得到字段
for (c = 1; c <= intCols; c++)
{
dt.Columns.Add(new DataColumn((String)values[1, c]));
} //赋值
for (r = 2; r <= intRows; r++)
{
DataRow dr = dt.NewRow();
for (c = 1; c <= intCols; c++)
{
dr[(String)values[1, c]] = values[r, c];
}
dt.Rows.Add(dr);
}
}
}
ds.Tables.Add(dt);
}
}
catch (Exception ex)
{
throw (ex);
}
finally
{
excel.Quit();
int generation = System.GC.GetGeneration(excel);
System.GC.Collect(generation);
}
return ds;
}
public void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = false;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true); //将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
} //将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
//xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8));
xlBook.SaveCopyAs(strFileName); xlApp.Workbooks.Close();
xlApp.Quit();
int generation = System.GC.GetGeneration(xlApp);
System.GC.Collect(generation);
}
}
}C# excel com lib dll
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货