分享

求用winform导入导出excel的方法

 昵称16649133 2014-04-03

求用winform导入导出excel的方法!!!另外我在调试时出错!!!

2010-12-02 15:43 MEHAOKE | 分类:C#/.NET | 浏览2397次
谁能给我发关于何使用winform导入导出excel方法具体实现winform表格直接和excel关联关于数据库导出excelexcel嵌入winform需要窗口切换要用控件
调试错误希望高手帮忙
命名空间Microsoft.Office类型或命名空间名称Interop(缺少程序集引用?) D:\vs\Excel\Excel\Form1.cs
错误该解决
2010-12-02 18:08 提问者采纳
首先 先确定安装了EXCEL没有安装了 
VS能会自动添加组件 能没有添加
关系 因组件自己添加

步 添加引用 也添加组件 所有解决方案里都有引用文件夹
里点击添加引用 会看选项卡界面 点击浏览
把excel文件夹下 Microsoft.Office.Interop.Excel.dll组件添加进去

再cs文件还得写 using Microsoft.Office.Interop.Excel;
具体导入EXCEL 或 导出代码了 我里做了导入EXCEL
希望能帮

public void ExportTOExcel()
{
if (dbgname.Rows.Count == 0)
{
MessageBox.Show("没有数据供导出", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
ToolStripProgressBar toolStripProgressBar1 = new ToolStripProgressBar();
toolStripProgressBar1.Visible = true;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
excel.Visible = false;//若true导出时候会显示EXcel界面
if (excel == null)
{
MessageBox.Show("EXCEL无法启动", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "test";

//生成字段名称
for (int i = 0; i < dbgname.ColumnCount-8; i++)
{
excel.Cells[1, i + 1] = dbgname.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < dbgname.RowCount - 1; i++)
{
for (int j = 0; j < dbgname.ColumnCount - 8; j++)
{
if (dbgname[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + dbgname[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dbgname[j, i].Value.ToString();
}
}
toolStripProgressBar1.Value += 100 / dbgname.RowCount;
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

GC.Collect();
MessageBox.Show("数据已经成功导出:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Visible = false;
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多