private void ExcelBut_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
string ExcelFileName=””;
DataRow dr=[——列名信息——-];
try
{
ExcelFileName=Path.Combine(Request.PhysicalApplicationPath + “/ExcelFile”, “MyExcel.xls”);
//获取Excel需要的数据
ds=[——获得数据——-];
if (ds.Tables.Count==0)
{
Response.Write(““);
return;
}
int sheetNum=ds.Tables.Count / 2;
StreamWriter writer=new StreamWriter(ExcelFileName, false);
//Styles标签前面的信息相当于’头信息’,不需要改变
writer.WriteLine(”
“);
writer.WriteLine(”
“);
writer.WriteLine(“
writer.WriteLine(“xmlns:o=”urn:schemas-microsoft-com:office:office””);
writer.WriteLine(“xmlns:x=”urn:schemas-microsoft-com:office:excel””);
writer.WriteLine(“xmlns:ss=”urn:schemas-microsoft-com:office:spreadsheet””);
writer.WriteLine(“xmlns:html=”http://www.w3.org/TR/REC-html40″>”);
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(string.Format(“
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(“”);
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(“”);
//通过 ss:ID 和 ss:Name相当于html中style的类
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” ss:Color=’#FF0000’/> “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” ss:Color=’#FF0000’/> “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
writer.WriteLine(”
writer.WriteLine(” “);
writer.WriteLine(” “);
//Sheet操作
writer.WriteLine(“
writer.WriteLine(”
“);
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
for (int num=sheetNum – 1; num >=0; num=num – 1)
{
// 考課表間有3行的空行
if (num !=sheetNum – 1)
{
writer.WriteLine(“
writer.WriteLine(“”);
writer.WriteLine(“
writer.WriteLine(“”);
writer.WriteLine(“
writer.WriteLine(“”);
}
// 列名
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
// 详细数据
for (int i=0; i < ds.Tables[2 * num].Rows.Count; i++)
{
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
}
}
writer.WriteLine(” “);
writer.WriteLine(” “);
// 其他Sheet操作
for (int num=sheetNum – 1; num >=0; num–)
{
writer.WriteLine(“
writer.WriteLine(”
“);
writer.WriteLine(”
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
writer.WriteLine(”
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
// 列名
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
int SheetN=ds.Tables[2 * num].Rows.Count;
//详细数据
for (int i=0; i < SheetN; i++)
{
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
writer.WriteLine(“”);
}
writer.WriteLine(“
writer.WriteLine(“”);
writer.WriteLine(“
writer.WriteLine(“”);
//列名
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
int DateLen=ds.Tables[2 * num + 1].Rows.Count;
string EmployeeManagement=””;
for (int i=0; i < DateLen; i++)
{
writer.WriteLine(“
writer.WriteLine(“
if (!EmployeeManagement.Equals(ds.Tables[2 * num + 1].Rows[i][“EmployeeManagementID”].ToString()))
{
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“
EmployeeManagement=ds.Tables[2 * num + 1].Rows[i][“EmployeeManagementID”].ToString();
}
else
{
writer.WriteLine(“”);
writer.WriteLine(“”);
writer.WriteLine(“”);
writer.WriteLine(“”);
writer.WriteLine(“”);
writer.WriteLine(“”);
}
writer.WriteLine(“
writer.WriteLine(“
writer.WriteLine(“”);
}
writer.WriteLine(” “);
writer.WriteLine(” “);
}
writer.WriteLine(“”);
writer.Close();
FileDownload(ExcelFileName);
}
catch (System.Exception ex)
{
}
finally
{
Response.End();
}
}
public void FileDownload(FullFileName)
{
FileInfo DownloadFile=new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=true;
Response.AppendHeader(“Content-Disposition”, “attachment;filename=” + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(MyFile”.xls”)));
Response.ContentType=”application/ms-excel;charset=UTF-8″;//DownloadFile.FullNameoctet-stream
Response.ContentEncoding=System.Text.Encoding.GetEncoding(“shift-jis”);
Response.AppendHeader(“Content-Length”, DownloadFile.Length.ToString());
Response.WriteFile(FullFileName);
Response.Flush();
if (File.Exists(FullFileName))
{
File.Delete(FullFileName);
}
}