private void Button1_Click(object sender, System.EventArgs e) { DataTable SourceTb = new DataTable(); DataColumn myDataColumn;
myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "RowIndex"; //序 号 SourceTb.Columns.Add(myDataColumn);
myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpManName"; //审批人 SourceTb.Columns.Add(myDataColumn);
myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpIdeas"; //审批意见 SourceTb.Columns.Add(myDataColumn);
myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpDate"; //审批时间 SourceTb.Columns.Add(myDataColumn);
myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpRole"; //审批岗位 SourceTb.Columns.Add(myDataColumn);
myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "HandleType"; //操作类型(1:提交| 9:驳回) SourceTb.Columns.Add(myDataColumn);
DataRow myDataRow; for(int i = 0;i < 30;i ++) { myDataRow = SourceTb.NewRow(); myDataRow["RowIndex"] = i.ToString(); myDataRow["CheckUpManName"] = "张三"; myDataRow["CheckUpIdeas"] = "同意"; myDataRow["CheckUpDate"] = "2006-03-20"; myDataRow["CheckUpRole"] = "物资部主任"; SourceTb.Rows.Add(myDataRow); }
//绑定数据到DataGrid1 this.DataGrid1.DataSource = SourceTb.DefaultView; this.DataGrid1.DataBind(); //将DataGrid1构成的html代码写进StringWriter this.DataGrid1.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); this.DataGrid1.RenderControl(hw); string HtmlInfo = tw.ToString().Trim();
string DocFileName = "审批信息.xls"; string FilePathName = Request.PhysicalPath; FilePathName = FilePathName.Substring(0,FilePathName.LastIndexOf("//")); //得到Excel文件的物理地址 FilePathName = FilePathName +"//" + DocFileName; System.IO.File.Delete(FilePathName); FileStream Fs = new FileStream(FilePathName, FileMode.Create); BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("GB18030")); //将DataGrid的信息写入Excel文件 BWriter.Write(HtmlInfo); BWriter.Close(); Fs.Close(); }
|