将数据绑定到datagrid上后,如何把datagrid内容打印出来
如:输出到用excel、word的形式打印出来,
要求不高,只要按照datagrid的样式打印就可以,且能够分页
有vb代码的高手提供一下,谢谢了!
如:输出到用excel、word的形式打印出来,
要求不高,只要按照datagrid的样式打印就可以,且能够分页
有vb代码的高手提供一下,谢谢了!
解决方案 »
- 导出Excel的时候如何去掉制表符?
- 这个表格怎么实现
- 从数据库怎样读出图片
- 生成静态页的问题,很经典,受用终身
- 请问在C#中的一个函数中调用前台的Javascript 要怎么用:
- 当datagrid没有绑定数据的时候,怎么样不显示页数?现在的情况是即使没有数据页数的位置也显示1??
- 关于Image
- help!框架页面刷新问题
- 同一个用户控件,不同的加载方式,怎么呈现不一样的效果~~
- dll文件
- 问题还未解决 再问孟子等高人 用代码创建的Datagrid 模版列并加有一 LinkButton 控件 有事件返加但还是没有执行DATAGRIDK中ItemCommand事
- 初学ASP。NET大家给点意见,给点学习资料和网站也行,只顶的没有分~~
问题解决立刻结贴。
Excel.Sheets sheets=myBook.Worksheets;
Excel.Worksheet mySheet=(Excel.Worksheet)sheets["dd"];
mySheet.Activate(); int xrow=6;
mySheet.Cells [6,1]=dataset4.Tables ["dj"].Rows [0]["项目名称"].ToString ();
xrow=xrow+1;
int signid=0;
for (int xk=0;xk<dataset4.Tables ["dj1"].Rows.Count ;xk++)
{
if (xk==0)
{ mySheet.Cells [1,7]=strgcmc+"——投标文件";
mySheet.Cells [2,7]="工程编号:"+strgcbh;
}
else
{
mySheet.Cells [xrow-6,7]=strgcmc+"—";
mySheet.Cells [xrow-5,7]="工程编号:"+strgcbh;
mySheet.Cells [xrow-1,1]=dataset4.Tables ["dj1"].Rows [xk]["项目名称"].ToString ();
}
int snn=1;
for (int xi=signid;xi<dataset4.Tables ["dj"].Rows.Count ;xi++)
{
if (dataset4.Tables ["dj1"].Rows [xk]["dd"].ToString ()==dataset4.Tables ["dj"].Rows [xi]["dd"].ToString ())
{
int xcol=6; for (int xj=1;xj<7;xj++)
{
mySheet.Cells [xrow,4]=dataset4.Tables ["dj"].Rows [xi]["项目名称"].ToString ();
mySheet.Cells [xrow,xcol]=dataset4.Tables ["dj"].Rows [xi][xj].ToString ();
xcol=xcol+1;
}
xrow=xrow+1;
signid=signid+1;
}
else
{
break;
}
snn=snn+1;
}
if (snn-23<0)
{
xrow=xrow+Math.Abs(snn-23);
}
xrow=xrow+19;
}
2、能不能控制一列的宽度,并使其自动换行?
1.先创建一个DataTable,作为数据来源,也可以另将其它的数据源。 Private Function CreaTable() As DataTable Dim dt As New DataTable() dt.Columns.Add("列1", GetType(String)) dt.Columns.Add("列2", GetType(Integer)) dt.Columns.Add("列3", GetType(String)) dt.Columns.Add("列4", GetType(String)) Dim row, row1 As DataRow row = dt.NewRow() row!列1 = "行1" row!列2 = 1 row!列3 = "d" row!列4 = "a" dt.Rows.Add(row) row1 = dt.NewRow() row1!列1 = "行2" row1!列2 = 12 row1!列3 = "b" row1!列4 = "c" dt.Rows.Add(row1) Return dt End Function2.将表中的内容导出到Excel Dim xlApp As New Excel.Application() Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim rowIndex, colIndex As Integer rowIndex = 1 colIndex = 0 xlBook = xlApp.Workbooks().Add xlSheet = xlBook.Worksheets("sheet1") Dim Table As New DataTable() Table = CreaTable() '将所得到的表的列名,赋值给单元格 Dim Col As DataColumn Dim Row As DataRow For Each Col In Table.Columns colIndex = colIndex + 1 xlApp.Cells(1, colIndex) = Col.ColumnName Next '得到的表所有行,赋值给单元格 For Each Row In Table.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each Col In Table.Columns colIndex = colIndex + 1 xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName) Next Next With xlSheet .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体" '设标题为黑体字 .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True '标题字体加粗 .Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1 '设表格边框样式 End With With xlSheet.PageSetup .LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc .CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:" .RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:" .LeftFooter = "&""楷体_GB2312,常规""&10制表人:" .CenterFooter = "&""楷体_GB2312,常规""&10制表日期:" .RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页" End With xlApp.Visible = True
{
// HttpContext.Current.Response.Charset ="GB2312";
HttpContext.Current.Response.Charset ="";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=money.xls");
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}然后再调用:ToExcel(datagrid1)
{
//
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
GC.Collect();
Application excel;// = new Application();
int rowIndex=4;
int colIndex=1; _Workbook xBk;
_Worksheet xSt; excel= new ApplicationClass();
xBk = excel.Workbooks.Add(true);
xSt = (_Worksheet)xBk.ActiveSheet; //
//取得标题
//
foreach(DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
} //
//取得表格中的数据
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
excel.Cells[rowSum,2] = "合计";
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//设置选中的部分的颜色
//
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
excel.Cells[2,2] = str;
//
//设置整个报表的标题格式
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
//
//显示效果
//
excel.Visible=true; //xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
xBk.SaveCopyAs(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls"); ds = null;
xBk.Close(false, null,null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath(this.xlfile.Text+".xls"); System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();
}