C#程序中操作Excel的问题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#程序中操作Excel的问题?相关的知识,希望对你有一定的参考价值。
怎样把程序中的表格式是数据导出到Excel中去,Excel 的api文档那里有啊,
多给点分吗? 这是一个sample。。。。public partial class Window1 : Window
DataTable dt = new DataTable();
object misValue = System.Reflection.Missing.Value;
public Window1()
InitializeComponent();
dt.Columns.Add("AA", typeof(string));
dt.Columns.Add("BB", typeof(string));
dt.Rows.Add("11", "22");
dt.Rows.Add("33", "44");
this.dataGrid1.ItemsSource = ((IListSource)dt).GetList();
private void Button_Click(object sender, RoutedEventArgs e)
#region
Microsoft.Office.Interop.Excel.Application app=null;
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Worksheet ws=null;
try
app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Add(misValue);
ws = app.ActiveWorkbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Range range;
range = ws.get_Range("A2", misValue);
range = range.get_Resize(dt.Rows.Count, dt.Columns.Count);
Microsoft.Office.Interop.Excel.Range columnNameRange;
columnNameRange = ws.get_Range("A1", misValue);
columnNameRange = columnNameRange.get_Resize(1, dt.Columns.Count);
string[] columNames = new string[dt.Columns.Count];
for(int i=0;i<dt.Columns.Count;i++)
columNames[i] = dt.Columns[i].ColumnName;
columnNameRange.set_Value(misValue, columNames);
string[,] arr = new string[dt.Rows.Count, dt.Columns.Count];
for (int i = 0; i < dt.Rows.Count; i++)
for (int j = 0; j < dt.Columns.Count; j++)
arr[i, j] = dt.Rows[i][j].ToString();
range.set_Value(misValue, arr);
wb.Close(true, "D:\\Test.xlsx", misValue);
catch (Exception ex)
Trace.WriteLine(ex.Message.ToString());
finally
// make sure the office process quit after certain operations done.
if (app != null)
app.UserControl = false;
app.Quit();
app = null;
#endregion
参考技术A C#貌似不需要API就可以做到,用连接字符串可以直接读取到文件的内容,不过写入就不太清楚了 参考技术B 这段代码 修改下 应该能用
/// <summary>
/// 执行导出
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名(详细路径加文件名)</param>
private static void doExport(DataSet ds, string strExcelFileName)
Excel.Application excel = new Excel.Application();
if (excel == null)
MessageBox.Show("无法启动Excel,可能您的电脑未安装Excel");
return;
try
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
foreach (DataRow row in table.Rows)
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
excel.Visible = false;
//excel.Sheets[0] = "消息";
excel.DisplayAlerts = false;
excel.ActiveWorkbook.SaveCopyAs(strExcelFileName);
excel.Quit();
excel = null;
MessageBox.Show("数据导出成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
catch (Exception ex)
MessageBox.Show(ex.ToString());
finally
GC.Collect();//垃圾回收
参考技术C //导出:
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
for (int i = 1; i <= dt.Columns.Count; i++)//打印表头
excel.Cells[1, i] = dt.Columns.ColumnName.ToString();
for (int i = 2; i <= dt.Rows.Count + 1; i++)//打印表的内容
for (int j = 1; j <= dt.Columns.Count; j++)
excel.Cells = dt.Rows[j - 1].ToString();
excel.Visible = true;
//导入:
if (this.DataGrid1.Items.Count != 0)
try
for (int i = 0; i < this.DataGrid1.Items.Count; i++)
model_result.CouseId = int.Parse(this.ddlCourse.SelectedValue.ToString());
model_result.StudentId = this.DataGrid1.Items.Cells[0].Text;
model_result.Terms = this.ddlTerms.SelectedItem.Text;
if (bll_grade.Judge_SomeOne_Student_Is_Have_Exist(model_result))
model_result.TotalScore = float.Parse(this.DataGrid1.Items.Cells[2].Text);
bll_grade.Insert_Student_TotalScore_Online(model_result);
Response.Write("<script>alert('导入成功!');</script>");
catch
Response.Write("<script>alert('导入失败!');</script>");
我空间的日志
以上是关于C#程序中操作Excel的问题?的主要内容,如果未能解决你的问题,请参考以下文章
C#中的Excel操作——设置Excel单元格的内容,打开Excel文件的一种方式
C# Excel 互操作:来自 HRESULT 的异常 (DISP_E_BADINDEX)
C# 自己写的Winform程序批量导入Excel文件到Oracle数据库的过程中,程序运行会很慢!而且Winform窗体会卡