一个DataGrid里有两张表的数据,导出成一张表

 protected void btnExcel_Click(object sender, EventArgs e)
{
InfoExport();
} protected void InfoExport()
{
try
{
string ExcelName = this.CreateExcel();
//将服务器上的Excel导出
// CuteWebUIOperate.DownloadFile(HttpContext.Current, Server.MapPath("ExcelExport/") + ExcelName, ExcelName, false);
string strScript = "window.open('ExcelExport/" + ExcelName + "');";
this.WriteAjaxMessage(strScript);
}
catch
{
throw;
}
} #region 导出excel
protected string CreateExcel() //生成Excel
{
string Header = "考试题库";
string strFileName = ""; // 生成文件夹
string fileFolderPath = Server.MapPath("ExcelExport/");
if (!System.IO.Directory.Exists(fileFolderPath))
System.IO.Directory.CreateDirectory(fileFolderPath); Workbook wb = new Workbook(); wb.Worksheets.Add("Sheet1"); Worksheet ws = wb.ActiveWorksheet;
WorksheetMergedCellsRegionCollection wm = ws.MergedCellsRegions;
#region
WorksheetMergedCellsRegion wmc = wm.Add(, , , );//起始位置和终止位置 wmc.Value = Header;
wmc.CellFormat.Alignment = HorizontalCellAlignment.Center;
wmc.CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
wmc.CellFormat.BottomBorderColor = Color.Black;
wmc.CellFormat.LeftBorderColor = Color.Black;
wmc.CellFormat.RightBorderColor = Color.Black;
wmc.CellFormat.TopBorderColor = Color.Black; wmc.CellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.WrapText = ExcelDefaultableBoolean.True;
wmc.CellFormat.Font.Name = "宋体";
//字体大小
wmc.CellFormat.Font.Height = ;
IWorksheetCellFormat HeadCellFormat = wb.CreateNewWorksheetCellFormat();
HeadCellFormat.Alignment = HorizontalCellAlignment.Center;
HeadCellFormat.Font.Bold = ExcelDefaultableBoolean.True;
HeadCellFormat.Font.Name = "宋体"; HeadCellFormat.BottomBorderColor = Color.Black;
HeadCellFormat.LeftBorderColor = Color.Black;
HeadCellFormat.RightBorderColor = Color.Black;
HeadCellFormat.TopBorderColor = Color.Black; HeadCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.WrapText = ExcelDefaultableBoolean.True; IWorksheetCellFormat ItemCellFormat = wb.CreateNewWorksheetCellFormat();
//CellFormat.Alignment = HorizontalCellAlignment.Center;
//CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
ItemCellFormat.FillPattern = FillPatternStyle.Default;
ItemCellFormat.ShrinkToFit = ExcelDefaultableBoolean.True;
ItemCellFormat.BottomBorderColor = Color.Black;
ItemCellFormat.LeftBorderColor = Color.Black;
ItemCellFormat.RightBorderColor = Color.Black;
ItemCellFormat.TopBorderColor = Color.Black; ItemCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.WrapText = ExcelDefaultableBoolean.True;
ItemCellFormat.FormatString = "##,##0.00";
ItemCellFormat.Font.Name = "宋体";
#endregion
int n;
n = ;
#region
wmc = wm.Add(, n, , n++);
wmc.Value = "序号";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "题目名称";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "题目类型";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "题目难度";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "答案1";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "答案2";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "答案3";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "答案4";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "答案5";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "答案6";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "正确答案";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "参考答案";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "答案解析";
wmc.CellFormat.SetFormatting(HeadCellFormat);
#endregion int Groupid = Convert.ToInt32(Request.QueryString["ParentRowID"]);
//List<M_Exam_Subject> list_s = bll.SelectAll().FindAll(x => x.GroupID == Groupid); List<M_Exam_Subject> list_s = bll.SelectByGroupID(Groupid);
for (int i = ; i < list_s.Count; i++)
{
string tmSubjectGuid = list_s[i].SubjectGuid.ToString(); n = ; wmc = wm.Add(i + , n, i + , n++);
wmc.Value = Convert.ToString(i + );
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = list_s[i].Title.ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
string type = list_s[i].Type.ToString();
wmc.Value = GetType(type);
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
string difficult = list_s[i].Difficult.ToString();
wmc.Value = GetDifficult(difficult);
wmc.CellFormat.SetFormatting(ItemCellFormat);
#region 导出Exam_Answer表答案
//DataView da = GetExcelDaAn(tmSubjectGuid);
List<M_Exam_Answer> list_a = bllAnswer.SelectBySubjectGuid(tmSubjectGuid);
if (list_a.Count > )
{
for (int j = ; j < list_a.Count; j++)
{
wmc = wm.Add(i + , n, i + , n++);
wmc.Value = list_a[j].AnswerName.ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat);
}
for (int m = ; m < ( - list_a.Count); m++)
{
wmc = wm.Add(i + , n, i + , n++);
wmc.Value = "";
wmc.CellFormat.SetFormatting(ItemCellFormat);
} }
else
for (int j = ; j < ; j++)
{
wmc = wm.Add(i + , n, i + , n++);
wmc.Value = "";
wmc.CellFormat.SetFormatting(ItemCellFormat);
}
#endregion //导出Exam_Answer表答案IsRight
string isright = "";
List<M_Exam_Answer> list_aw = bllAnswer.SelectBySubjectGuid(tmSubjectGuid).FindAll(x => x.IsRight == isright);
string stranswer_kg = "";
if (list_aw.Count > )
{
int j = ;
for (int m = ; m < list_aw.Count; m++)
{
stranswer_kg += j.ToString() + "." + list_aw[m].AnswerName.ToString();
j++;
}
wmc = wm.Add(i + , n, i + , n++);
wmc.Value = stranswer_kg;
wmc.CellFormat.SetFormatting(ItemCellFormat);
}
else
for (int m = ; m < ; m++)
{
wmc = wm.Add(i + , n, i + , n++);
wmc.Value = "";
wmc.CellFormat.SetFormatting(ItemCellFormat);
} wmc = wm.Add(i + , n, i + , n++);
wmc.Value = string.IsNullOrEmpty(list_s[i].RightAnswer) ? "" : list_s[i].RightAnswer.ToString();//参考答案
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = string.IsNullOrEmpty(list_s[i].AnswerNote) ? "" : list_s[i].AnswerNote.ToString();// list_s[i].AnswerNote.ToString();//答案解析
wmc.CellFormat.SetFormatting(ItemCellFormat);
} string mark = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
strFileName = "Export_" + mark + ".xls";
BIFF8Writer.WriteWorkbookToFile(wb, Server.MapPath("ExcelExport/" + strFileName)); return strFileName;
}
#endregion

导出整个DataGrid

 protected void InfoExport()
{
try
{
string ExcelName = this.CreateExcel();
//将服务器上的Excel导出
// CuteWebUIOperate.DownloadFile(HttpContext.Current, Server.MapPath("ExcelExport/") + ExcelName, ExcelName, false);
string strScript = "window.open('ExcelExport/" + ExcelName + "');";
this.WriteAjaxMessage(strScript);
}
catch
{
throw;
}
} protected string CreateExcel() //生成Excel
{
string Header = "批次信息";
string strFileName = ""; // 生成文件夹
string fileFolderPath = Server.MapPath("ExcelExport/");
if (!System.IO.Directory.Exists(fileFolderPath))
System.IO.Directory.CreateDirectory(fileFolderPath); Workbook wb = new Workbook(); wb.Worksheets.Add("Sheet1"); Worksheet ws = wb.ActiveWorksheet;
//first row 19cell
WorksheetMergedCellsRegionCollection wm = ws.MergedCellsRegions; WorksheetMergedCellsRegion wmc = wm.Add(, , , );//起始位置和终止位置
wmc.Value = Header;
wmc.CellFormat.Alignment = HorizontalCellAlignment.Center;
wmc.CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
wmc.CellFormat.BottomBorderColor = Color.Black;
wmc.CellFormat.LeftBorderColor = Color.Black;
wmc.CellFormat.RightBorderColor = Color.Black;
wmc.CellFormat.TopBorderColor = Color.Black; wmc.CellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.WrapText = ExcelDefaultableBoolean.True;
wmc.CellFormat.Font.Name = "宋体";
//字体大小
wmc.CellFormat.Font.Height = ;
IWorksheetCellFormat HeadCellFormat = wb.CreateNewWorksheetCellFormat();
HeadCellFormat.Alignment = HorizontalCellAlignment.Center;
HeadCellFormat.Font.Bold = ExcelDefaultableBoolean.True;
HeadCellFormat.Font.Name = "宋体"; HeadCellFormat.BottomBorderColor = Color.Black;
HeadCellFormat.LeftBorderColor = Color.Black;
HeadCellFormat.RightBorderColor = Color.Black;
HeadCellFormat.TopBorderColor = Color.Black; HeadCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.WrapText = ExcelDefaultableBoolean.True; IWorksheetCellFormat ItemCellFormat = wb.CreateNewWorksheetCellFormat();
//CellFormat.Alignment = HorizontalCellAlignment.Center;
//CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
ItemCellFormat.FillPattern = FillPatternStyle.Default;
ItemCellFormat.ShrinkToFit = ExcelDefaultableBoolean.True;
ItemCellFormat.BottomBorderColor = Color.Black;
ItemCellFormat.LeftBorderColor = Color.Black;
ItemCellFormat.RightBorderColor = Color.Black;
ItemCellFormat.TopBorderColor = Color.Black; ItemCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.WrapText = ExcelDefaultableBoolean.True;
ItemCellFormat.FormatString = "##,##0.00";
ItemCellFormat.Font.Name = "宋体";
int n;
n = ;
//wmc = wm.Add(1, n, 1, n++);
//wmc.Value = "序号";
//wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "企业名称";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "企业地址";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "所属分局";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "报名日期";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "姓名";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "性别";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "出生日期";
wmc.CellFormat.SetFormatting(HeadCellFormat); ws.Columns[n].Width = * ;
wmc = wm.Add(, n, , n++);
wmc.Value = "身份证号码";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "手机号码";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "是否已交费";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "交费日期";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "是否已安排培训";
wmc.CellFormat.SetFormatting(HeadCellFormat); ws.Columns[n].Width = * ;
wmc = wm.Add(, n, , n++);
wmc.Value = "培训批次";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "培训开始日期";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "培训结束日期";
wmc.CellFormat.SetFormatting(HeadCellFormat); DataView dv = GetExcelData();//获取报名信息
for (int i = ; i < dv.Count; i++)
{
n = ; //wmc = wm.Add(i + 2, n, i + 2, n++);
//wmc.Value = Convert.ToString(i + 1);
//wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["DanWeiName"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["DetailAddress"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = DB_Common.GetCodeName("PX_分局名称", dv[i]["BelongFenJu"].ToString());
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["BMAddDate"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["Name"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = DB_Common.GetCodeName("PX_性别", dv[i]["Sex"].ToString());
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["BirthDay"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["IdentityNum"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["MobilePhone"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = DB_Common.GetCodeName("PX_是否", dv[i]["IsPay"].ToString());
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["PayDate"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = DB_Common.GetCodeName("PX_是否", dv[i]["IsPeiXun"].ToString());
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["BaoMPiCi"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["PXStartDate"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["PXEndDate"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); } string mark = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
strFileName = "Export_" + mark + ".xls";
BIFF8Writer.WriteWorkbookToFile(wb, Server.MapPath("ExcelExport/" + strFileName)); return strFileName;
}

C# 导出到Excel的更多相关文章

  1. DataTable导出到Excel

    简单的导出到Excel中: 代码如下: using System; using System.Collections.Generic; using System.Data; using System. ...

  2. ASP.NET中GridView数据导出到Excel

    /// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...

  3. 【Java EE 学习 17 下】【数据库导出到Excel】【多条件查询方法】

    一.导出到Excel 1.使用DatabaseMetaData分析数据库的数据结构和相关信息. (1)测试得到所有数据库名: private static DataSource ds=DataSour ...

  4. PB的datawindow导出到excel文件(使用saveasascii)

    **********************************************************//*函数名称:uf_dwsaveas_excel功能:将数据窗口数据导出EXCEL ...

  5. 纯JS 将table表格导出到excel

    html <div > <button type="button" onclick="getXlsFromTbl('tableExcel','myDiv ...

  6. 将页面上的内容导出到Excel

    <asp:Button ID="lkbExport" runat="server" Name="Save" Text="导出 ...

  7. asp.net教程:GridView导出到Excel或Word文件

    asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...

  8. DataGridView导出到Excel的三个方法

    #region DataGridView数据显示到Excel /// <summary> /// 打开Excel并将DataGridView控件中数据导出到Excel /// </s ...

  9. salesforce 零基础学习(二十三)数据记录导出至excel(自定义报表导出)

    我们都知道,报表有个功能为导出excel,但是有的时候客户需求往往标准的报表达不到,比如导出excel,其中本月修改的数据字段标红,如下图所示. 这就需要我们去写VF来实现此功能. 需求:将数据表记录 ...

  10. HTML Table导出为Excel的方法

    HTML Table导出为Excel的方法: 直接上源码 <html> <head> <meta http-equiv="Content-Type" ...

随机推荐

  1. Core Data

    •   Core Data   是 iOS SDK   里的一个很强大的框架,允许程序员 以面向对象 的方式储存和管理数据 .使用 Core Data 框架,程序员可以很轻松有效 地通过面向对象的接口 ...

  2. cygwin配置git

    对于windows用户来说,使用git bash经常会出现乱码情况,那么一款优质高尚的软件,值得推荐一下了,那就是cygwin 下载cygwin后,在安装过程中,安装git,安装vim编辑器 然后会在 ...

  3. python: 模块发布

    一.准备发布 1.为模块文件创建一个文件夹,并将模块文件复制到这个文件中(一般,文件夹的名字和模块的名字一样) 2.在文件夹中创建一个名为『setup.py』的文件,内容如下: #encoding:u ...

  4. jsoup: Java HTML Parser (类似jquery)

    jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extr ...

  5. iOS - Xcode 配置

    1.Xcode 配置 1.1 OS X 1)main 文件注释修改路径: /Applications(应用程序) ▸ Xcode.app ▸ Contents ▸ Developer ▸ Librar ...

  6. Spring—Quartz定时调度CronTrigger时间配置格式说明与实例

    1 .CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 ...

  7. 【转】 void与void*详解

    void关键字的使用规则: 1. 如果函数没有返回值,那么应声明为void类型: 2. 如果函数无参数,那么应声明其参数为void: 3. 如果函数的参数可以是任意类型指针,那么应声明其参数为void ...

  8. matlab实现感知机算法--统计学习小灶

    clear all; clc; %% %算法 %输入:训练数据集T = {(x1,y1),(x2,y2),...,(xn,yn)};学习率η %输出:w,b;感知机模型f(x) = sign(w*x+ ...

  9. Java环境环境配置

    1. 下载并安装JDK,假如安装在D盘 2. 右键计算机->属性->高级系统设置->环境变量 3. 点击新建系统变量:系统变量名:JAVA_HOME   系统变量值:D:\Progr ...

  10. 注册Github

    注册Github 1.打开Github网页 2.设置用户名.邮箱.密码(右侧会显示是否可以使用),点击注册 3.此时邮箱会发来来自Github的注册消息,进入邮箱,点连接,完成注册 4.注册成功