小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

解決ASP.NET導(dǎo)出Excel文件時(shí) 用Excel2007打開時(shí)彈出文件類型與擴(kuò)展名不同的對話框

 趨明 2012-03-28
  protected void btnExport_Click(object sender, EventArgs e)
3     {
4         string strMapPath = Server.MapPath("~/");                                                       //獲取Web應(yīng)用程序的物理路徑
5          string sourceExcelFileName = strMapPath + "Temp.xls";                                           //源Excel文件名
6          string targetExcelFileName = strMapPath + @"TempExcel\" + Guid.NewGuid().ToString() + ".xls";   //使用Guid生成全局唯一字符串,作為目標(biāo)Excel文件的文件名
7  
8         File.Copy(sourceExcelFileName, targetExcelFileName);                                            //Copy文件,放在目標(biāo)文件夾中
9
10         //Excel模型,用來操作Excel文件
11          Microsoft.Office.Interop.Excel.Application excelApp = null;
12         Microsoft.Office.Interop.Excel.Workbook excelWb = null;
13         Microsoft.Office.Interop.Excel.Worksheet excelWs = null;
14         Microsoft.Office.Interop.Excel.Range excelR = null;
15
16         try
17         {
18             excelApp = new Microsoft.Office.Interop.Excel.Application();
19             excelWb = excelApp.Workbooks.Open(targetExcelFileName);                             //打開Excel工作簿文件
20             excelWs = (Microsoft.Office.Interop.Excel.Worksheet)(excelWb.Sheets.get_Item(1));   //選擇工作簿中第一個(gè)工作表
21
22             //向Excel中添加列名
23             for (int i = 0; i < dtbl.Columns.Count; i++)
24             {
25                 string rangeName = ((Char)(i + 65)).ToString() + "1";       //計(jì)算出單元格的位置(例:第一行第二列在Excel中為B1,即為值為1+65的字符+"1"。)
26                 excelR = excelWs.get_Range(rangeName);
27                 excelR.Value = dtbl.Columns[i].ColumnName;                  //在對應(yīng)單元格中寫入值
28             }
29
30             //把每一行數(shù)據(jù)寫入Excel模型中
31             for (int i = 0; i < dtbl.Rows.Count; i++)
32             {
33                 for (int j = 0; j < dtbl.Columns.Count; j++)
34                 {
35                     string rangName = ((Char)(j + 65)).ToString() + (i + 2).ToString();     //表格內(nèi)容的寫入從第二行開始
36                     excelR = excelWs.get_Range(rangName);
37                     excelR.Value = dtbl.Rows[i][j];                                         //在Excel中寫入對應(yīng)單元格的內(nèi)容
38                 }
39             }
40             excelWb.Save();
41
42         }
43         catch (Exception)
44         {
45             Response.Write("<script type='text/javascript'>alert('生成失??!');</script>");
46         }
47         finally
48         {
49             //關(guān)閉Excel,否則Excel文件將無法被打開
50             excelWb.Close();
51             excelApp.Workbooks.Close();
52             excelApp.Quit();
53         }
54
55         //向客戶端發(fā)送文件...
56         Response.Clear();
57         Response.AddHeader("Content-Disposition", "attachment;filename=excel.xls");     //設(shè)置回發(fā)內(nèi)容為Excel
58         Response.ContentType = "application/ms-excel";
59         Response.WriteFile(targetExcelFileName);                                        //把剛剛生成的Excel文件寫入Http流
60         Response.End();
61
62     }


 

2.由于會在臨時(shí)文件夾中存儲Excel文件,所以寫了一個(gè)類用來清理過期不用的Excel文件:

1 /// <summary>
2 /// TempExcelCleaner 清理TempExcel文件夾中已經(jīng)不用的臨時(shí)Excel文件
3 /// </summary>
4 public class TempExcelCleaner
5 {
6 private string tempDirectoryPath;
7 private int overtime = 30;
8 private int sleeptime = 60;
9
10 /// <summary>
11 /// 每次掃描文件夾的間隔時(shí)間,單位分鐘
12 /// </summary>
13 public int Sleeptime
14 {
15 get { return sleeptime; }
16 set { sleeptime = value; }
17 }
18
19 /// <summary>
20 /// 文件過期時(shí)間,單位分鐘
21 /// </summary>
22 public int Overtime
23 {
24 get { return overtime; }
25 set { overtime = value; }
26 }
27
28 /// <summary>
29 /// 構(gòu)造TempExcelCleaner
30 /// </summary>
31 /// <param name="tempDirectoryPath">要清理的文件夾路徑</param>
32 public TempExcelCleaner(string tempDirectoryPath)
33 {
34 this.tempDirectoryPath = tempDirectoryPath;
35 }
36
37 private TempExcelCleaner()
38 {
39 }
40
41 /// <summary>
42 /// 運(yùn)行,開始清理指定文件夾
43 /// </summary>
44 public void Run()
45 {
46 DirectoryInfo directory = new DirectoryInfo(tempDirectoryPath);
47 //遍歷文件夾中所有文件,超過指定時(shí)間就刪除
48 foreach (FileInfo file in directory.GetFiles())
49 {
50 DateTime createTime = file.CreationTime;
51 if (createTime.AddMinutes(overtime) < DateTime.Now)
52 {
53 file.Delete();
54 }
55 }
56 //遍歷完所有文件,休息指定時(shí)間
57 System.Threading.Thread.Sleep(sleeptime * 60 * 1000);
58 }
59 }

3.在Golobal.asax中的代碼,在Web應(yīng)用程序啟動(dòng)時(shí)開始臨時(shí)文件清理的服務(wù):

1 void Application_Start(object sender, EventArgs e)
2 {
3 // 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼
4 // Web應(yīng)用程序啟動(dòng)時(shí)開始執(zhí)行清理程序。
5 System.Threading.Thread threadTempExcelCleaner = new System.Threading.Thread(new System.Threading.ThreadStart(FunCleanTempExcel));
6 threadTempExcelCleaner.IsBackground = true;
7 threadTempExcelCleaner.Start();
8 }
9
10 void FunCleanTempExcel()
11 {
12 TempExcelCleaner cleaner = new TempExcelCleaner(@"F:\Users\Administrator\Documents\Visual Studio 2010\WebSites\ExportExcel\TempExcel");
13 cleaner.Run();
14 }

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多