用excel做比较好弄。当然最方便还是写成csv文件,嘿嘿。不过csv文件没有格式,不用看,打印什么的还得自己调整一下。
所以用excel比较方便,你就在页面上放个按钮,叫“导出报表”什么的,后台代码就是将页面上的统计数据写到excel中就可以了。而且你的excel是模板,所以,你自己也知道什么数据该写在什么位置。
下面给你一个删除EXCEL的小代码,呵呵,其他的修改,添加什么的都差不多,希望对你有用。
代码很简单,如同不懂的,直接自己调试一次就明白了。
程序代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application exc;
Workbooks workbooks;
Sheets sheets = null;
_Workbook workbook;
exc = new Microsoft.Office.Interop.Excel.Application();
workbooks = exc.Workbooks;
try
{
exc.Visible = false;
exc.DisplayAlerts = false;
exc.Interactive = false;
DateTime beforeTime = DateTime.Now;
string modePath = @"e:\a.xls";
string outputPath = @"e:\b.xls";
workbook = exc.Workbooks.Open(modePath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets[1];
worksheet.Activate();
Microsoft.Office.Interop.Excel.Range m_objRange = ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns[12,Missing.Value]);
//m_objRange.ColumnWidth = 1;
m_objRange.EntireColumn.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlToLeft);
workbook.SaveAs(outputPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, null, null, null, null, null);
workbook.Saved = true;
Response.Write("OK");
}
catch (Exception exp)
{
Response.Write(exp.ToString());
}
finally
{ //关闭和excel相关的东西
workbooks.Close();
exc.Quit();
workbooks = null;
workbook = null;
exc = null;
}
}
}