public override void VerifyRenderingInServerForm(Control control)
{}
/// <summary>
/// エクセル着力イベント
/// </summary>
/// <param></param>
/// <param></param>
private void ExcelBut_Click(object sender, System.EventArgs e)
{
DataGrid dgExcel = new DataGrid();
try
{
DataSet ds = getExcelData(this.PageParams.Value);//着力データを取得する
if(ds.Tables[0].Rows.Count>0)
{
//エクセルへデータを投入する
string execlName= "MyExcel";
Encoding encodingType=System.Text.Encoding.UTF8;
dgExcel.DataMember=ds.Tables[0].TableName;
dgExcel.DataSource=ds.Tables[0];
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + execlName+ ".xls");
Response.ContentEncoding = encodingType;
Response.ContentType = "application/ms-excel";
StringWriter oStringWriter = new StringWriter();
HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
dgExcel.DataBind();
dgExcel.Visible = true;
dgExcel.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.Flush();
Response.Close();
dgExcel.DataSource = null;
dgExcel.Visible = false;
}
else
{
Response.Write("<script>alert('xxxxxx')</script>");
}
}
catch(Exception ex)
{
Response.Write("<script>alert('oooooo')</script>");
}
}
aspx部门
复制代码 代码如下:
<head runat="server">
<script type="text/javascript">
//Excel DownLoad
function excelExport(){
var hidText = document.getElementById("hidParam");
hidText.value = "some params";
document.getElementById("ExcelOutput").click();
}
</script>
</head>
<body>
<form runat="server">
<input type="button" value="EXCEL下载"/>
<input type="text" runat="server"/>
<asp:Button runat="server" Text= "EXCEL着力" UseSubmitBehavior="false"/>
<asp:datagrid Visible="False" Runat="server"
Width="80px"></asp:datagrid>
</form>
</body>
要领三 以XML形式的Excel方法(可以配置富厚的样式,并可以有多个sheet,但需要模版。速度很快,但生成的文件较大,且无法保存VBA代码)
详细要领:把模版以 XML Document形式另存为A.xml。然后参照A.xml中的内容即可
复制代码 代码如下:
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("<script type='text/javascript'>alert('无数据');</script>");
return;
}
int sheetNum = ds.Tables.Count / 2;
StreamWriter writer = new StreamWriter(ExcelFileName, false);
//Styles标签前面的信息相当于'头信息',不需要改变
writer.WriteLine("<?xml version=\"1.0\"?>");
writer.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
writer.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
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("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
writer.WriteLine("<LastAuthor>Automated Report Generator Example</LastAuthor>");
writer.WriteLine(string.Format("<Created>{0}T{1}Z</Created>", DateTime.Now.ToString("yyyy-mm-dd"), DateTime.Now.ToString("HH:MM:SS")));
writer.WriteLine(" <Company>51aspx.com</Company>");
writer.WriteLine(" <Version>11.6408</Version>");
writer.WriteLine("</DocumentProperties>");
writer.WriteLine("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
writer.WriteLine("<WindowHeight>6195</WindowHeight>");
writer.WriteLine(" <WindowWidth>18495</WindowWidth>");
writer.WriteLine(" <WindowTopX>525</WindowTopX>");
writer.WriteLine(" <WindowTopY>4260</WindowTopY>");
writer.WriteLine(" <AcceptLabelsInFormulas/>");
writer.WriteLine(" <ProtectStructure>True</ProtectStructure>");
writer.WriteLine(" <ProtectWindows>False</ProtectWindows>");
writer.WriteLine("</ExcelWorkbook>");
//通过 ss:ID 和 ss:Name相当于html中style的类
writer.WriteLine(" <Styles> ");
writer.WriteLine(" <Style ss:ID='Default' ss:Name='Normal'> ");
writer.WriteLine(" <Alignment ss:Vertical='Bottom'/> ");
writer.WriteLine(" <Borders/> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" <NumberFormat/> ");
writer.WriteLine(" <Protection/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s23'> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11' ");
writer.WriteLine(" ss:Color='#FF0000'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s24'> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s25'> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s26'> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11' ");
writer.WriteLine(" ss:Color='#FF0000'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s27'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior ss:Color='#CCFFFF' ss:Pattern='Solid'/> ");
writer.WriteLine(" <Protection/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s28'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior ss:Color='#CCFFFF' ss:Pattern='Solid'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s29'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior ss:Color='#CCFFFF' ss:Pattern='Solid'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s30'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior ss:Color='#CCFFFF' ss:Pattern='Solid'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s31'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior ss:Color='#CCFFFF' ss:Pattern='Solid'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s32'> ");
writer.WriteLine(" <Alignment ss:Vertical='Bottom' ss:WrapText='1'/> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior ss:Color='#CCFFFF' ss:Pattern='Solid'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s33'> ");
writer.WriteLine(" <Alignment ss:Vertical='Bottom' ss:WrapText='1'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s34'> ");
writer.WriteLine(" <Alignment ss:Vertical='Bottom' ss:WrapText='1'/> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s35'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s36'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior ss:Color='#FFFF99' ss:Pattern='Solid'/> ");
writer.WriteLine(" <Protection ss:Protected='0'/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s37'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s38'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" <Style ss:ID='s39'> ");
writer.WriteLine(" <Borders> ");
writer.WriteLine(" <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/> ");
writer.WriteLine(" </Borders> ");
writer.WriteLine(" <Font ss:FontName='MS Pゴシック' x:CharSet='128' x:Family='Modern' ss:Size='11'/> ");
writer.WriteLine(" <Interior/> ");
writer.WriteLine(" </Style> ");
writer.WriteLine(" </Styles> ");
//Sheet操纵
writer.WriteLine("<Worksheet ss:Name='" + dr["Msg030"] + "'>");
writer.WriteLine(" <Table ss:ExpandedColumnCount='9' ss:ExpandedRowCount='3000' x:FullColumns='1' x:FullRows='1' ss:StyleID='s33' ss:DefaultColumnWidth='54' ss:DefaultRowHeight='13.5'>");
writer.WriteLine("<Column ss:StyleID='s34' ss:AutoFitWidth='0' ss:Width='110.25'/>");
writer.WriteLine("<Column ss:StyleID='s34' ss:AutoFitWidth='0' ss:Width='33.75'/>");
writer.WriteLine("<Column ss:StyleID='s34' ss:AutoFitWidth='0' ss:Width='118.5'/>");
writer.WriteLine("<Column ss:StyleID='s34' ss:AutoFitWidth='0' ss:Width='159.75'/>");
writer.WriteLine("<Column ss:StyleID='s34' ss:AutoFitWidth='0' ss:Width='147.75' ss:Span='4'/>");
for (int num = sheetNum - 1; num >= 0; num = num - 1)
{
// 考課表間有3行的空行
if (num != sheetNum - 1)
{
writer.WriteLine("<Row>");
writer.WriteLine("</Row>");
writer.WriteLine("<Row>");
writer.WriteLine("</Row>");
writer.WriteLine("<Row>");
writer.WriteLine("</Row>");
}
// 列名
writer.WriteLine("<Row ss:Height='40.5'>");
writer.WriteLine("<Cell ss:StyleID='s32'><Data ss:Type='String'>" + dr["Msg002"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s32'><Data ss:Type='String'>" + dr["Msg003"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s32'><Data ss:Type='String'>" + dr["Msg004"] + "</Data></Cell>");
writer.WriteLine("</Row>");
// 具体数据
for (int i = 0; i < ds.Tables[2 * num].Rows.Count; i++)
{
writer.WriteLine("<Row ss:Height='27'>");
writer.WriteLine("<Cell><Data ss:Type='String'>" + ds.Tables[2 * num].Rows[i]["EvalLevel1Name"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell><Data ss:Type='Number'>" + ds.Tables[2 * num].Rows[i]["EvalLevel2Code"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell><Data ss:Type='String'>" + ds.Tables[2 * num].Rows[i]["EvalLevel2Name"].ToString() + "</Data></Cell>");
writer.WriteLine("</Row>");
}
}
writer.WriteLine(" </Table>");
writer.WriteLine(" </Worksheet>");
// 其他Sheet操纵
for (int num = sheetNum - 1; num >= 0; num--)
{
writer.WriteLine("<Worksheet ss:Name='" + ds.Tables[2 * num].Rows[0][1].ToString() + "' ss:Protected='1'>");
writer.WriteLine(" <Table ss:ExpandedColumnCount='31' ss:ExpandedRowCount='30000' x:FullColumns='1' x:FullRows='1' ss:StyleID='s25' ss:DefaultColumnWidth='54' ss:DefaultRowHeight='13.5'>");
writer.WriteLine(" <Row>");
writer.WriteLine("<Cell ss:StyleID='s26'><PhoneticText xmlns='urn:schemas-microsoft-com:office:excel'>カキキイロテンスウツ</PhoneticText><Data ss:Type='String'>" + dr["Msg031"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:Index='14' ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("</Row>");
writer.WriteLine(" <Row>");
writer.WriteLine("<Cell ss:StyleID='s26'><PhoneticText xmlns='urn:schemas-microsoft-com:office:excel'>カキキイロテンスウツ</PhoneticText><Data ss:Type='String'>" + dr["Msg032"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:Index='14' ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("<Cell ss:StyleID='Default'/>");
writer.WriteLine("</Row>");
// 列名
writer.WriteLine("<Row>");
writer.WriteLine("<Cell ss:StyleID='s27'><Data ss:Type='String'>" + dr["Msg001"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s28'><Data ss:Type='String'>" + dr["Msg002"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s28'><Data ss:Type='String'>" + dr["Msg003"] + "</Data></Cell>");
writer.WriteLine("</Row>");
int SheetN = ds.Tables[2 * num].Rows.Count;
//具体数据
for (int i = 0; i < SheetN; i++)
{
writer.WriteLine("<Row>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='Number'>" + ds.Tables[2 * num].Rows[i]["EvalLevel1Code"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'>" + ds.Tables[2 * num].Rows[i]["EvalLevel1Name"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='Number'>" + ds.Tables[2 * num].Rows[i]["EvalLevel2Code"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell><Data ss:Type='String'> </Data></Cell>");
writer.WriteLine("</Row>");
}
writer.WriteLine("<Row>");
writer.WriteLine("</Row>");
writer.WriteLine("<Row>");
writer.WriteLine("</Row>");
//列名
writer.WriteLine("<Row>");
writer.WriteLine("<Cell ss:StyleID='s29'><Data ss:Type='String'>" + dr["Msg011"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s30'/>");
writer.WriteLine("<Cell ss:StyleID='s31'/>");
writer.WriteLine("<Cell ss:StyleID='s29'><Data ss:Type='String'>" + dr["Msg012"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s31'/>");
writer.WriteLine("<Cell ss:StyleID='s29'><Data ss:Type='String'>" + dr["Msg013"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s30'/>");
writer.WriteLine("<Cell ss:StyleID='s30'/>");
writer.WriteLine("<Cell ss:StyleID='s30'/>");
writer.WriteLine("<Cell ss:StyleID='s31'/>");
writer.WriteLine("<Cell ss:StyleID='s29'><Data ss:Type='String'>" + dr["Msg014"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s31'/>");
writer.WriteLine("<Cell ss:StyleID='s29'><Data ss:Type='String'>" + dr["Msg015"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s31'/>");
writer.WriteLine("</Row>");
writer.WriteLine("<Row>");
writer.WriteLine("<Cell ss:StyleID='s28'><Data ss:Type='String'>" + dr["Msg016"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s28'><Data ss:Type='String'>" + dr["Msg017"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s28'><Data ss:Type='String'>" + dr["Msg018"] + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s28'><Data ss:Type='String'>" + dr["Msg019"] + "</Data></Cell>");
writer.WriteLine("</Row>");
int DateLen = ds.Tables[2 * num + 1].Rows.Count;
string EmployeeManagement = "";
for (int i = 0; i < DateLen; i++)
{
writer.WriteLine("<Row>");
writer.WriteLine("<Cell ss:StyleID='s37'><Data ss:Type='Number'>" + ds.Tables[2 * num + 1].Rows[i]["EmployeeManagementID"].ToString() + "</Data></Cell>");
if (!EmployeeManagement.Equals(ds.Tables[2 * num + 1].Rows[i]["EmployeeManagementID"].ToString()))
{
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='Number'>" + ds.Tables[2 * num + 1].Rows[i]["EmployeeCode"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'>" + ds.Tables[2 * num + 1].Rows[i]["EmployeeName"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'>" + ds.Tables[2 * num + 1].Rows[i]["BefEmployeeName2"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'>" + ds.Tables[2 * num + 1].Rows[i]["EmployeeName2"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s38'><Data ss:Type='Number'>" + ds.Tables[2 * num + 1].Rows[i]["EvalLevel1Code"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'>" + ds.Tables[2 * num + 1].Rows[i]["EvalLevel1Name"].ToString() + "</Data></Cell>");
EmployeeManagement = ds.Tables[2 * num + 1].Rows[i]["EmployeeManagementID"].ToString();
}
else
{
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'></Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'></Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'></Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'></Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s38'><Data ss:Type='String'></Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'></Data></Cell>");
}
writer.WriteLine("<Cell ss:StyleID='s39'><Data ss:Type='Number'>" + ds.Tables[2 * num + 1].Rows[i]["EvalLevel2Code"].ToString() + "</Data></Cell>");
writer.WriteLine("<Cell ss:StyleID='s35'><Data ss:Type='String'>" + ds.Tables[2 * num + 1].Rows[i]["EvalLevel2Name"].ToString() + "</Data></Cell>");
writer.WriteLine("</Row>");
}
writer.WriteLine(" </Table>");
writer.WriteLine(" </Worksheet>");
}
writer.WriteLine("</Workbook>");
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);
}
}
要领四 用微软的COM组件操纵Excel。固然可以很利便的操纵单位格,而且能保存VBA代码,它相当于直接打开一个EXCEL历程。
当初这个COM组件式给WinForm筹备的,但在Web端有许多对象的支持不尽人意,譬喻无法用组件中的要领封锁Excel工具,