用ASP.NET2.0生成WORD EXCEL文档
我想用ASP.NET2.0,这样我们的测试报告等就可以在WEB系统上填写,并从系统上生成了
大家有何建议。
我在ASP.NET 1.0时处理不了的就是那些图片等。。。。
[解决办法]
//处理EXCEL 代码是可以运行的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.Button btnCreateExcel;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
SqlConnection Conn=new SqlConnection( "server=(local);uid=sa;pwd=sa;database=pubs ");
SqlDataAdapter da;
DataSet ds=new DataSet();
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnLoad = new System.Windows.Forms.Button();
this.btnCreateExcel = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = " ";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 16);
this.dataGrid1.Name = "dataGrid1 ";
this.dataGrid1.Size = new System.Drawing.Size(304, 184);
this.dataGrid1.TabIndex = 0;
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(48, 232);
this.btnLoad.Name = "btnLoad ";
this.btnLoad.TabIndex = 1;
this.btnLoad.Text = "加载 ";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// btnCreateExcel
//
this.btnCreateExcel.Location = new System.Drawing.Point(176, 232);
this.btnCreateExcel.Name = "btnCreateExcel ";
this.btnCreateExcel.TabIndex = 2;
this.btnCreateExcel.Text = "导入Excel ";
this.btnCreateExcel.Click += new System.EventHandler(this.btnCreateExcel_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 273);
this.Controls.Add(this.btnCreateExcel);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1 ";
this.Text = "Form1 ";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void LoadData()
{
da=new SqlDataAdapter( "select * from publishers ",Conn);
da.Fill(ds, "publishers ");
dataGrid1.DataSource=ds.Tables[0].DefaultView;
}
private void Form1_Load(object sender, System.EventArgs e)
{
Microsoft.Win32.RegistryKey key=Microsoft.Win32.Registry.CurrentUser.OpenSubKey( "SoftWare\\Microsoft\\Internet Explorer\\PageSetup ",true);
key.SetValue( "header ", " ");
key.SetValue( "footer ", " ");
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
LoadData();
}
private void CreateExcel()
{
Excel.ApplicationClass ExcelApp=new Excel.ApplicationClass();
ExcelApp.Visible=false;
Excel.Workbook ExcelWork=ExcelApp.Workbooks.Add(true);
Excel.Worksheet ExcelSheet=(Excel.Worksheet)ExcelWork.Worksheets[1];
Excel.Range range=ExcelSheet.get_Range( "B2 ", "F2 ");
range.Merge(0);
range.HorizontalAlignment=Excel.XlVAlign.xlVAlignCenter;
range.Font.Size=10;
ExcelSheet.Cells[2,2]= "数据测试 ";
int R=3;
for(int i=0;i <=ds.Tables[0].Columns.Count-1;i++)
{
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).Borders.LineStyle=1;
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).Font.Size=10;
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).ColumnWidth=14;
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).HorizontalAlignment=Excel.XlVAlign.xlVAlignCenter;
ExcelSheet.Cells[R,i+2]=ds.Tables[0].Columns[i].ColumnName;
}
R++;
for(int j=0;j <=ds.Tables[0].Rows.Count-1;j++)
{
for(int i=0;i <=ds.Tables[0].Columns.Count-1;i++)
{
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).Borders.LineStyle=1;
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).Font.Size=10;
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).ColumnWidth=14;
ExcelSheet.get_Range(ExcelSheet.Cells[R,i+2],ExcelSheet.Cells[R,i+2]).HorizontalAlignment=Excel.XlVAlign.xlVAlignCenter;
ExcelSheet.Cells[R,i+2]=ds.Tables[0].Rows[j][i];
}
R++;
}
ExcelApp.ActiveWorkbook.SaveAs( "c:\\review.htm ",Excel.XlFileFormat.xlHtml,null,null,null,
null,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
ExcelApp.Quit();
}
private void btnCreateExcel_Click(object sender, System.EventArgs e)
{
CreateExcel();
Form2 f=new Form2();
f.Show();
}
}
}