using Excel;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
public class PrintEnXsl
{
public PrintEnXsl()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string TempEnXsl;
public bool ProduceXsl(string ItemName)
{//生成英文成绩单
Excel.Application oExcel;//= new Excel.Application();
Excel.Workbooks oBooks;
Excel.Workbook oBook;
Excel.Sheets oSheets;
try
{
SqlConnection con = DB.createConnection();
con.Open();
//查找该学生基本信息
string mysql = "select * from tbEmailsMessage where ItemName='" + ItemName + "' and state=5";
//string mysqlScore = "select * from tbStuScores where StuId='" + StuId + "'";
SqlDataAdapter da = new SqlDataAdapter(mysql, con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
//table[0] 成绩信息,table[1]个人信息
System.Data.DataTable dt = ds.Tables[0];//
if (dt.Rows.Count > 0)
{
//初始化 excel
oExcel = new Application();
if (oExcel == null)
{
return false;
//Response.Write("<script>alert('不能初始化Excel应用程序!')</script>");
}
else
{
string sFile, sTemplate;
sFile = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "//Temp//" + HttpContext.Current.Session["UserName"] + "En.xls";
sTemplate = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "//TemplateXsl//Template.xls";
HttpContext.Current.Session["FilePath"] = "Temp/" + HttpContext.Current.Session["UserName"] + "En.xls";
//TempChXsl = sFile;
//HttpContext.Current.Session["FilePath"] = sFile;
//是否可见,调试时 可以为 true
//oExcel.Visible = true;
//oExcel.DisplayAlerts = true;
oExcel.Visible = false;
oExcel.DisplayAlerts = false;
if (File.Exists(sFile))
{//判断临时文件是否存在,是就删除
File.Delete(sFile);
}
//
oBooks = oExcel.Workbooks;
oBooks.Add(sTemplate);//打开模板工作簿
oBook = oBooks.get_Item(1);
//
oSheets = oBook.Worksheets;
DumpData(ds, oSheets);
oBook.SaveAs(sFile, Excel.XlFileFormat.xlXMLSpreadsheet, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
oExcel.Quit();
//File.Delete(sFile);//删除临时文件
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
return true;
}
}
else
{
return false;
}
}
catch (SqlException sqlerror)
{
HttpContext.Current.Response.Redirect("ErrorPage.aspx?Err=" + sqlerror.Message.Replace("/n", ""), true);
return false;
}
catch (Exception e1)
{
//Response.Redirect("ErrorPage.aspx?Err='" + Server.UrlEncode(e1.Message)+"'");
HttpContext.Current.Response.Redirect("ErrorPage.aspx?Err=" + e1.Message.Replace("/n", ""), true);
return false;
}
finally
{
oExcel = null;
oBooks = null;
oBook = null;
oSheets = null;
System.GC.Collect();
}
//return false;
}
private void DumpData(DataSet ds, Excel.Sheets oSheets)
{
try
{
Worksheet oSheetEn;
Excel.Range oCellsEn;
//string sFile = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "//Temp//MyExcel.xls";
System.Data.DataTable dt = ds.Tables[0];//信息
oSheetEn = (Excel.Worksheet)oSheets.get_Item(1);//模板
oSheetEn.Name = "FirstEn";
oCellsEn = oSheetEn.Cells;
int j = 3;
oCellsEn[1, 1] ="项目名:"+dt.Rows[0]["ItemName"].ToString();
for (int i = 0; i < dt.Rows.Count; i++)
{
oCellsEn[j, 1] = "问题 " + i + ":" + dt.Rows[i]["QuestionName"].ToString();
oCellsEn[j, 2] = dt.Rows[i]["QuestionContent"].ToString();
oCellsEn[j, 3] ="答 :"+ dt.Rows[i]["Answers"].ToString();
j = j + 2;
}
//oCells[i + BeginRow, BeginCol] = StuCurEnName.Substring(0, 21);
//dumpPublicEn(oCellsEn, dtBasic);
//this.DumpScoresFourEn(dtScore, length, oCellsEn, startYear);//英文
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheetEn);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oCellsEn);
oSheetEn = null;
oCellsEn = null;
}
finally
{
}
}
}