我想在exportbutton_Click中填加一些代码,实现把datagridview显示的数据导入到excel
下面是我的程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;namespace SMLReportSystem
{
partial class MAERSK : Form
{
public MAERSK(SMLReportSystem.SMLReport parent)
{
InitializeComponent(); this.MdiParent = parent;
} private void fillToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.pr_MAERSKTableAdapter.Fill(this.sMLRPTDataSet.pr_MAERSK, new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(dateTimePickerStart.Text, typeof(System.DateTime))))), new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(dateTimePickerEnd.Text, typeof(System.DateTime))))));
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
} private void exportbutton_Click(object sender, EventArgs e)
{ }
}
}
小妹初级,分少,希望大家不要嫌弃
下面是我的程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;namespace SMLReportSystem
{
partial class MAERSK : Form
{
public MAERSK(SMLReportSystem.SMLReport parent)
{
InitializeComponent(); this.MdiParent = parent;
} private void fillToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.pr_MAERSKTableAdapter.Fill(this.sMLRPTDataSet.pr_MAERSK, new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(dateTimePickerStart.Text, typeof(System.DateTime))))), new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(dateTimePickerEnd.Text, typeof(System.DateTime))))));
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
} private void exportbutton_Click(object sender, EventArgs e)
{ }
}
}
小妹初级,分少,希望大家不要嫌弃
解决方案 »
- 关于wp7/silverlight/wpf/winform的移植问题.
- 利用 Visual C# .NET 的自动化功能在 Word 中创建新文档
- 3个字节表示一个ShortSingle问题
- c# oledb add添加数据到数据库,参数未声明
- 如何制作服务呢,请大家给个建议,谢谢
- WINForm,我用GDI+画图比如画一条直线,再画一个圆。画完之后我怎么选中以前画的图进行修改删除等两个问题?
- 调用API问题。
- wpf 线程
- 为什么ocx控件的部分属性的值不能改变?
- 今天安装,又遇到vs.net2003的安装问题……
- 依然在问类似问题.通过窗口句柄获取窗口类名,标题等.
- 做论坛的时候,回帖的时候可以引用楼上的内容,这种效果怎么实现好?
http://blog.csdn.net/simonllf/archive/2006/12/13/1441672.aspx
private void ToExcel_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.ApplicationClass MyExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
MyExcel.Visible = true;
if (MyExcel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int rowcount = 0;
int columncount = 0;
MyExcel.Application.Workbooks.Add(true);
columncount = this.show.ColumnCount;
rowcount = this.show.RowCount;
for (int m = 1; m < columncount; m++)
{
MyExcel.Cells[1, m] = this.show.Columns[m-1].HeaderText;//去除show的编号列
}
for (int i = 0; i < rowcount; i++)
{
for (int j = 1; j < columncount; j++)
{
MyExcel.Cells[i + 2, j] = Convert.ToString(this.show[j-1, i].Value);
} }
运行提示错误:“SMLReportSystem.MAERSK”并不包含“show”的定义
我都不知道怎么声明"show"......希望大家多多帮忙,谢谢大家了!
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog saveFileDlg = new SaveFileDialog();
saveFileDlg.FileName = "报废信息";//默认文件名
saveFileDlg.DefaultExt = "xls";//默认后缀名
saveFileDlg.Filter = "Excel文件 (*.xls)|*.xls"; if (saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
doExport(rollInfoDs1, saveFileDlg.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统管理", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void doExport(DataSet ds, string strExcelFileName)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
if (excel == null)
throw new Exception("无法启动Excel,可能您的计算机尚未安装Excel");
int rowIndex = 1;
int colIndex = 0; excel.Application.Workbooks.Add(true); System.Data.DataTable table = ds.Tables["ROLLINFO"];
if (table != null && table.Rows.Count > 0)
{
DataTable dataTableNew = new DataTable();
dataTableNew.Columns.Add("序号");
dataTableNew.Columns.Add("钢号");
dataTableNew.Columns.Add("现场编号");
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow dr = dataTableNew.NewRow();
dr["序号"] = table.Rows[i]["XH"].ToString();
dr["钢号"] = table.Rows[i]["ROLL_ID"].ToString();
dr["现场编号"] = table.Rows[i]["ROLL_NUM"].ToString();
dataTableNew.Rows.Add(dr);
} foreach (DataColumn col in dataTableNew.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in dataTableNew.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dataTableNew.Columns)
{
colIndex++;
excel.Rows.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
excel.Visible = false;
try
{
excel.ActiveWorkbook.SaveAs(strExcelFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
}
catch (Exception ex)
{
throw ex;
}
excel.Quit();
excel = null; GC.Collect();//垃圾回收
}
{
System.Windows.Forms.SaveFileDialog saveFileDlg = new SaveFileDialog();
saveFileDlg.FileName = "报废信息";//默认文件名
saveFileDlg.DefaultExt = "xls";//默认后缀名
saveFileDlg.Filter = "Excel文件 (*.xls) ¦*.xls"; if (saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
doExport(saveFileDlg.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统管理", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
} private void doExport(string strExcelFileName)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
if (excel == null)
throw new Exception("无法启动Excel,可能您的计算机尚未安装Excel");
int rowIndex = 1;
int colIndex = 0; excel.Application.Workbooks.Add(true);
if (dataGridView1 != null && dataGridView1.Rows.Count > 0)
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
colIndex++;
excel.Cells[1, colIndex] = dataGridView1.Columns[i].Name;
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
colIndex++;
excel.Rows.Cells[rowIndex, colIndex] = row.Cells[col.Name].Value != null ? row.Cells[col.Name].Value.ToString() : "";
}
}
} excel.Visible = false;
try
{
excel.ActiveWorkbook.SaveAs(strExcelFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
}
catch (Exception ex)
{
throw ex;
}
excel.Quit();
excel = null; GC.Collect();//垃圾回收
}
提供代码的网页上有注明show是datagridview类型的变量,可是我不知道怎么声明,希望大家给点帮助,谢谢大家了
下面是我的程序的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;namespace SMLReportSystem
{
partial class MAERSK : Form
{
public MAERSK(SMLReportSystem.SMLReport parent)
{
InitializeComponent(); this.MdiParent = parent;
} private void fillToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.pr_MAERSKTableAdapter.Fill(this.sMLRPTDataSet.pr_MAERSK, new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(dateTimePickerStart.Text, typeof(System.DateTime))))), new System.Nullable<System.DateTime>(((System.DateTime)(System.Convert.ChangeType(dateTimePickerEnd.Text, typeof(System.DateTime))))));
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
} private void exportbutton_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.ApplicationClass MyExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
MyExcel.Visible = true;
if (MyExcel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int rowcount = 0;
int columncount = 0;
MyExcel.Application.Workbooks.Add(true);
columncount = this.show.ColumnCount;
rowcount = this.show.RowCount;
for (int m = 1; m < columncount; m++)
{
MyExcel.Cells[1, m] = this.show.Columns[m - 1].HeaderText;//去除show的编号列
}
for (int i = 0; i < rowcount; i++)
{
for (int j = 1; j < columncount; j++)
{
MyExcel.Cells[i + 2, j] = Convert.ToString(this.show[j - 1, i].Value);
} }
}
}
}
大国,它提示我这个怎么办啊?
应用程序中发生了无法处理的异常.如果单击继续应用程序将忽略此错误并尝试继续.如果单击退出应用程序将立即关闭.
提供的筛选器字符串无效.筛选器字符串必须包含筛选器的说明,后跟竖线(|)和筛选模式.不同筛选悬想的字符串还必须以竖线分隔.例如:"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*".