一个"打印"按钮的事件该怎么写?(单击该按钮会把dataGridView里的数据打印出来)
一个"保存"按钮的事件该怎么写?(单击该按钮会把dataGridView里的数据保存为Excel出来,麻烦高手顺便再教教我怎么指定这个Excel文件的保存路径)
小弟新手,不胜感激
一个"保存"按钮的事件该怎么写?(单击该按钮会把dataGridView里的数据保存为Excel出来,麻烦高手顺便再教教我怎么指定这个Excel文件的保存路径)
小弟新手,不胜感激
解决方案 »
- C#正则表达式替换HTML标签,除<IMG>以外的所有都替换
- InstallShield 2009 设置和获取ComboBox的值
- 主流开发的基础必备知识有哪些,作为程序员如何归纳整理!贴常用代码或举例说明,散分!
- 请问DataGrid里面的数字数据怎么靠右边显示啊?
- 一个关于Dictionary比较的问题,,
- 哪个大哥用过install sheld打包过asp.net请指导一下啊!
- 请问在winform的datagrid中绑定的有一列字段是图像二进制数据,用什么方法将图像在cell里显示出来最好?
- socket 简单问题,各位大哥帮忙解决解决.
- COMBOBOX怎么绑定选项和真实值?
- 如何获取datagrid中的新增行向数据库中插入数据
- winform datagridview中的单元格如何添加一个datetimepicker
- 图片上传控件使用出现的问题!!!!!!请前辈指导
public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
return false;
//建立Excel
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
}
return true;
}
#endregion
function PreviewPrint()
{
bdhtml=window.document.body.innerHTML;
sprnstr="<!--startprint-->";
eprnstr="<!--endprint-->";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
var obj=document.getElementById("GridView1");
if (obj!=null)
{
var rows = obj.rows;
var hide=rows.length-1;
obj.rows(hide).style.display = "none";
}
window.print();
window.parent.returnValue='0';
}
function NoPagePrint()
{
bdhtml=window.document.body.innerHTML;
sprnstr="<!--startprint-->";
eprnstr="<!--endprint-->";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
window.parent.returnValue='0';
window.parent.close();
}
function PreviewPrintCancel()
{
window.parent.returnValue='0';
window.parent.close();
}
function CloseWin()
{
window.parent.returnValue='1';
window.parent.close();
}
function DoNothingAndCloseWin()
{
window.parent.returnValue='DoNothing';
}在把 <!--startprint--> <!--endprint-->加在dataGridView 上
{
Response.ContentType = "application/ms-word";
Response.AddHeader("Content-Disposition", "inline;filename=DocumentPrint.doc");
StringBuilder sb = new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append("<html><body>");
this.PrintA.RenderControl(hw);
sb.Append("</body></html>");
Response.Write(sb.ToString());
Response.End();
}
System.Data.DataTable dt = new System.Data.DataTable();//否则会出现错误:“DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用
if (ds.Tables[0].Rows.Count <= 0)
{
MessageBox.Show("没有该记录!");
return;
}
else
{
//导出学生信息到EXCEL中
//************** 也可先将数据放入到数组中,然后再写入EXCEL中即可。这样可使速度提高。数据量较大时建议采用此方法。
ApplicationClass acExcel;//声明EXCEL应用程序对象
Workbook wb;//声明工作簿对象
Worksheet ws;//声明工作表对象
acExcel = new ApplicationClass();
acExcel.Visible = true;
if (acExcel == null)
{
MessageBox.Show("EXCEL无法启动!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
wb = acExcel.Workbooks.Add(true);//新建工作簿对象
ws = (Worksheet)wb.Worksheets[1];//引用工作簿中的第1个工作表
//设置EXCEL表格中开头显示的内容
ws.Cells[1, 2] = lblCompany.Text;//在第1行第2个单元格中显示内容
ws.Cells[2, 1] = "公司名称" + cbxCompany.Text;
ws.Cells[3, 1] = "打印日期:" + DateTime.Now.ToLongDateString();
//设置行和列的索引
int rowindex = 5;//从第5行、第1列开始显示数据
int colindex = 1;
foreach (DataColumn dc in dt.Columns)
{
//添加列名
ws.Cells[rowindex, colindex] = dc.ColumnName;//第五行开始显示列名
colindex++;
}
int rowCount = dt.Rows.Count;
int colCount = dt.Columns.Count;
ws.get_Range(ws.Cells[rowindex + 1, 1], ws.Cells[rowCount + rowindex, colCount]).NumberFormatLocal = "@";//设置显示的文本为字符格式,以防长数字出现科学记数法的数字形式
foreach (DataRow dr in dt.Rows)
{
rowindex++;
colindex = 1;
foreach (DataColumn dc in dt.Columns)
{
ws.Cells[rowindex, colindex] = dr[dc.ColumnName].ToString();
}
}
}