日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

从DataTable导出Excel,并下载,删除Excel进程。

發布時間:2023/12/18 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从DataTable导出Excel,并下载,删除Excel进程。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

頁面上加入一個button即可.
源代碼如下:

using?System;
using?System.Collections;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Web;
using?System.Web.SessionState;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.HtmlControls;
using?Microsoft.Office.Interop.Excel;

namespace?Test
{
????
/**////?<summary>
????
///?TestExcel?的摘要說明。
????
///?</summary>

????public?class?TestExcel?:?System.Web.UI.Page
????
{
????????
protected?System.Web.UI.WebControls.Button?Button1;
????
????????
private?void?Page_Load(object?sender,?System.EventArgs?e)
????????
{
????????????
????????}


????????
Web?窗體設計器生成的代碼#region?Web?窗體設計器生成的代碼
????????
override?protected?void?OnInit(EventArgs?e)
????????
{
????????????
//
????????????
//?CODEGEN:?該調用是?ASP.NET?Web?窗體設計器所必需的。
????????????
//
????????????InitializeComponent();
????????????
base.OnInit(e);
????????}

????????
????????
/**////?<summary>
????????
///?設計器支持所需的方法?-?不要使用代碼編輯器修改
????????
///?此方法的內容。
????????
///?</summary>

????????private?void?InitializeComponent()
????????
{????
????????????
this.Button1.Click?+=?new?System.EventHandler(this.Button1_Click);
????????????
this.Load?+=?new?System.EventHandler(this.Page_Load);

????????}

????????
#endregion


????????
private?void?Button1_Click(object?sender,?System.EventArgs?e)
????????
{
????????????
try
????????????
{
????????????????
string?DownloadPath=Server.MapPath(".");????//副本的文件夾路徑。
????????????????
//副本的文件名。
????????????????string?TempFileName?=?DateTime.Now.ToString("yyyyMMdd")?+?DateTime.Now.Hour?+?DateTime.Now.Minute?+?DateTime.Now.Second?+?".XLS";?
????????????????
this.txtTempFileName.Text=TempFileName;
????????????????
object?missing?=?System.Reflection.Missing.Value;
????????????????
object?missing2?=?System.Reflection.Missing.Value;
????????????????ApplicationClass?myExcel
=new?ApplicationClass();

????????????????Workbook?myBook
=(Workbook)myExcel.Workbooks.Add(missing);
????????????????Worksheet?curSheet?
=?(Worksheet)myBook.Sheets[1];
????????????????
//設置Excel樣式
????????????????Range?r1=(Range)myExcel.Cells[1,2];

????????????????Range?r2
=(Range)myExcel.Cells[3,4];

????????????????r1.Font.Bold
=true;
????????????????r2.Font.Bold
=true;


????????????????
string?DownloadFilePath=DownloadPath+"\\"+TempFileName;

?????????????????System.Data.DataTable?dt
=this.GetTable();
????????????????
int?rc=dt.Rows.Count;
????????????????
//繪制邊框
????????????????Range?rBorders=(Range)curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[7+rc+1,10]);
????????????????rBorders.Borders.LineStyle
=1;
????????????????curSheet.get_Range(myExcel.Cells[
7,1],myExcel.Cells[7+rc+1,1]).Borders[XlBordersIndex.xlEdgeLeft].Weight?=?XlBorderWeight.xlThick;//設置左邊線加粗
????????????????curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[7,10]).Borders[XlBordersIndex.xlEdgeTop].Weight?=?XlBorderWeight.xlThick;//設置設置上邊線加粗
????????????????curSheet.get_Range(myExcel.Cells[7+rc+1,1],myExcel.Cells[7+rc+1,10]).Borders[XlBordersIndex.xlEdgeBottom].Weight?=?XlBorderWeight.xlThick;//設置下邊線加粗
????????????????curSheet.get_Range(myExcel.Cells[7,10],myExcel.Cells[7+rc+1,10]).Borders[XlBordersIndex.xlEdgeRight].Weight?=?XlBorderWeight.xlThick;//設置右邊線加粗



????????????????
//Excel的表頭信息
????????????????myExcel.Cells[1,2]="表頭信息";

????????????????myExcel.Cells[
2,2]="New?Added:"?+?DateTime.Now.ToString();
????????????????myExcel.Cells[
3,4]="VENDOR??CODE??LIST??-??BY??PRODUCTS.";
????????????????myExcel.Cells[
4,4]="****************************************";

????????????????myExcel.Cells[
5,9]="DATE?From:"?+?DateTime.Now.ToString();
????????????????myExcel.Cells[
6,9]="DATE?To:"?+?DateTime.Now.ToString();

????????????????myExcel.Cells[
7,5]="PARTS?SUPPLIER";

????????????????
//設置Excel表列頭
????????????????myExcel.Cells[8,1]="Item";
????????????????myExcel.Cells[
8,2]="OrgCode";
????????????????myExcel.Cells[
8,3]="VendorCode";
????????????????myExcel.Cells[
8,4]="VendorName";
????????????????myExcel.Cells[
8,5]="A";
????????????????myExcel.Cells[
8,6]="B";
????????????????myExcel.Cells[
8,7]="C";
????????????????myExcel.Cells[
8,8]="PayMentType";
????????????????myExcel.Cells[
8,9]="TermsCode";
????????????????myExcel.Cells[
8,10]="CreateTime";
????????????????
//設置表頭字體風格
????????????????curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[8,10]).Font.Bold=true;
????????????????
int?j=1;//j為總結的Item數目的變量
????????????????int?i=9;
????????????????
while?(i-8<=dt.Rows.Count)
????????????????
{
????????????????????myExcel.Cells[i,
1]=j.ToString();
????????????????????myExcel.Cells[i,
2]=dt.Rows[i-9]["Name"].ToString().Trim();
????????????????????myExcel.Cells[i,
3]=dt.Rows[i-9]["cost"].ToString().Trim();
????????????????????myExcel.Cells[i,
4]=dt.Rows[i-9]["bug"].ToString().Trim();
????????????????????myExcel.Cells[i,
5]="";
????????????????????myExcel.Cells[i,
6]="";
????????????????????myExcel.Cells[i,
7]="";
????????????????????
????????????????????
//設置顏色,否則日期顯示成"######"格式。
????????????????????Range?rCol10=(Range)myExcel.Cells[i,10];
????????????????????rCol10
=null;
????????????????????
//從1開始循環
????????????????????j++;
????????????????????i
++;
????????????????}
?

????????????????myBook.Saved
=true;
????????????????myBook.SaveAs(DownloadFilePath,missing2,
"","",false,false,XlSaveAsAccessMode.xlNoChange,1,false,missing,missing,missing);

????????????????myBook.Close(
false,?null,null);
????????????????myExcel.Quit();
????????????????System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
????????????????System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
????????????????myBook?
=?null;
????????????????myExcel?
=?null;
????????????????GC.Collect();
????????????????
//下載文件
????????????????HttpResponse?response?=?HttpContext.Current.Response;?
????????????????response.Clear();
????????????????response.WriteFile(DownloadFilePath);
????????????????
string?httpHeader="attachment;filename=backup.Xls";
????????????????response.AppendHeader(
"Content-Disposition",?httpHeader);
????????????????response.Flush();
????????????????
//刪除臨時文件
????????????????System.IO.File.Delete(DownloadFilePath);
????????????????
????????????????killExcelProcess();
????????????}

????????????
catch(Exception?Ex)
????????????
{
????????????????
throw?Ex;
????????????}

????????}

????????
/**////?<summary>
????????
///?刪除Excel進程
????????
///?</summary>

????????private?void??killExcelProcess(){
????????????
//結束?Excel?進程
????????????foreach(System.Diagnostics.Process?xlProcess?in?System.Diagnostics.Process.GetProcesses()){
????????????????
if(?xlProcess.ProcessName.ToUpper().Equals("EXCEL"))?{
????????????????????
//結束?excel?進程?
????????????????????xlProcess.Kill();
????????????????}

????????????}

????????????????
????????}

????????
/**////?<summary>
????????
///?構建臨時DataTable
????????
///?</summary>
????????
///?<returns></returns>

????????private?System.Data.DataTable?GetTable()
????????
{
????????????System.Data.DataTable?dt?
=?new?System.Data.DataTable();
????????????dt.Columns.Add(
"Name");
????????????dt.Columns.Add(
"cost");
????????????dt.Columns.Add(
"bug");
????????????DataRow?rw?
=?dt.NewRow();
????????????rw[
"Name"]=?"";
????????????rw[
"Cost"]=?"12";
????????????rw[
"bug"]=?"5";
????????????dt.Rows.Add(rw);
????????????rw?
=?dt.NewRow();
????????????rw[
"Name"]=?"";
????????????rw[
"Cost"]=?"15";
????????????rw[
"bug"]=?"2";
????????????dt.Rows.Add(rw);
????????????rw?
=?dt.NewRow();
????????????rw[
"Name"]=?"";
????????????rw[
"Cost"]=?"8";
????????????rw[
"bug"]=?"1";
????????????dt.Rows.Add(rw);
????????????
return?dt;
????????}

????}

}


?

轉載于:https://www.cnblogs.com/echo/archive/2005/05/24/161461.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的从DataTable导出Excel,并下载,删除Excel进程。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。