asp.net 如何导出07excel

 我来答
万菡uU
推荐于2016-05-08 · TA获得超过258个赞
知道小有建树答主
回答量:379
采纳率:66%
帮助的人:380万
展开全部

先引用

先引用
using System.Security;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Configuration;

直接导出方法1.

public override void VerifyRenderingInServerForm(Control control)//导出Excel必须加整个不然会报错
    {
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GV1.Visible = true;
        string filename = "SendMailHistory";
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "UTF-8";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        Response.ContentType = "application/vnd.ms-excel; charset=UTF-8";
        EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        GV1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }

方法2用模版


protected void Button4_Click(object sender, EventArgs e)
    {
        
        string save_path = "", tick = "";
       
        ExcelOperate excelOperate = new ExcelOperate();
        string temp_path = Server.MapPath("~/DownLoad/Report");//生成的文件存放路径
        string loadfilename=Server.MapPath("~/DownLoad/Report/SendMailHistory.xlsx");
        string template_path = Server.MapPath("~/designer");//模板路径
        if (!Directory.Exists(temp_path))
        {
            Directory.CreateDirectory(temp_path);
            Directory.CreateDirectory(template_path);
        }
        try
        {
            if (File.Exists(loadfilename))
            {
                File.Delete(loadfilename);
            }
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            if (app == null)
            {
               
            }
            app.Visible = false;
            app.UserControl = true;
            Workbooks workbooks = app.Workbooks;
            _Workbook workbook = workbooks.Add(template_path + "\\sendmailhistory.xlsx");
            Sheets sheets = workbook.Worksheets;
            _Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
            if (worksheet == null)
            {
               
            }
            //======数据写入excel
          
            int rowNum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rowNum = i + 1;
                worksheet.Cells[6 + i, 1] = rowNum;
                worksheet.Cells[6 + i, 2] = dt.Rows[i]["wtemp"].ToString();
                worksheet.Cells[6 + i, 3] = dt.Rows[i]["YP_FormBG_ID"].ToString();
                worksheet.Cells[6 + i, 4] = dt.Rows[i]["PartNO"].ToString();
                worksheet.Cells[6 + i, 5] = dt.Rows[i]["ModelName"].ToString();
                worksheet.Cells[6 + i, 6] = dt.Rows[i]["SendUser"].ToString();
               // worksheet.get_Range(worksheet.Cells[14 + i, 7], worksheet.Cells[14 + i, 8]).Merge(); //合并单元格
                worksheet.Cells[6 + i, 7] = dt.Rows[i]["SendDate"].ToString();
                worksheet.Cells[6 + i, 8] = dt.Rows[i]["Mark1"].ToString();
                worksheet.get_Range(worksheet.Cells[6 + i, 1], worksheet.Cells[6 + i, 8]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                worksheet.get_Range(worksheet.Cells[6 + i, 1], worksheet.Cells[6 + i, 8]).Borders.LineStyle = XlLineStyle.xlContinuous;
                
            }
            worksheet.Cells[rowNum + 6 + 1, 3] = "编制:";//worksheet.Cells[rowNum +6 +1, 3] 
            worksheet.Cells[rowNum + 6 + 1, 5] = "审批:";
            worksheet.Cells[rowNum + 6 + 1, 7] = "WL4E300002_AE";
            worksheet.get_Range(worksheet.Cells[rowNum + 6 + 1, 7], worksheet.Cells[rowNum + 6 + 1, 8]).Merge(); //合并单元格
 
            tick = DateTime.Now.Ticks.ToString();
            save_path = loadfilename;
            workbook.SaveAs(save_path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            excelOperate.Dispose(worksheet, workbook, app);//关闭Excel进程
            labmsg.Text = "导出Excel成功";
            Response.Redirect("~/DownLoad/Report/SendMailHistory.xlsx");
        }
        catch (Exception ex)
        {
            labmsg.Text = "";
            labmsg.ForeColor = Color.Red;
            Response.Redirect("~/DownLoad/Report/SendMailHistory.xlsx");
           // s = false;
        }
    }
江城Spring
2013-06-13 · TA获得超过1202个赞
知道小有建树答主
回答量:763
采纳率:0%
帮助的人:353万
展开全部
public static bool ExportToExcel(DataTable table, string 
excelName, int[] columnIndexs, string[] columnHeads)
{
#region 将方法中用到的所有Excel变量声明在方法最开始,以便最后统一回收。
Excel.ApplicationClass oExcel = new 
Excel.ApplicationClass();
Excel.Workbook obook = null;
Excel.Worksheet oSheet = null;
Excel.Range 
range = null;
#endregion
try
{
obook = oExcel.Workbooks.Add("");
oSheet = (Excel.Worksheet)obook.Worksheets[1];
int rCount, cCount;
rCount = 
table.Rows.Count;
cCount = table.Columns.Count;
object obj = System.Reflection.Missing.Value;
if (cCount < columnIndexs.Length || cCount < 
columnHeads.Length)
{
throw new 
ArgumentOutOfRangeException("columnIndexs 与 columnHeads 长度必须一致。");
}
for (int i = 1; i <= 
columnIndexs.Length; i++)
{
//Excel.Range = (Excel.Range)oSheet.Columns.get_Item(i, obj); 

range = (Excel.Range)oSheet.Columns.get_Item(i, obj);
range.NumberFormatLocal = "@";
}
for (int c = 0; c < columnIndexs.Length; c++)
{
oSheet.Cells[1, c + 1] = 
columnHeads[c];
for (int r = 1; r <= rCount; r++)
{
oSheet.Cells[r + 1, c + 1] = table.Rows[r 
- 1][columnIndexs[c]].ToString();
}
}
obook.SaveCopyAs(excelName);
//必须调用 
obook.Close(), 否则无法释放进程。
obook.Close(false, 
System.Reflection.Missing.Value, System.Reflection.Missing.Value);
return true;
}
catch 
(Exception ex)
{
throw ex;
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(obook);
oExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
GC.Collect();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式