一个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;
}