asp.net 匯入和匯出Excel

來源:互聯網
上載者:User

匯出:
  匯入模板
        string filePath = Server.MapPath("~/TemplateFile/按天計薪模板.xls");
        string newpath = Server.MapPath("~/TemplateFile/" + Guid.NewGuid() + ".xls");
        File.Copy(filePath, newpath);
        File.SetAttributes(newpath, FileAttributes.Normal);
        Stream s = File.Open(newpath, FileMode.Open);
  //----------建立Excel
        HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook(s);
        s.Close(); 
        File.Delete(newpath);
        HSSFSheet sheet = wb.GetSheetAt(0);
        string strAdd = string.Empty;
        string strnull = string.Empty;
        int rowCount = sheet.LastRowNum - 1;
        StringBuilder sbError = new StringBuilder();
        HSSFCell cell;
        string where = " AND Dealed=1  AND AuditingState=’finished’  AND RewardType=’day’ " + strwhere;
        DataTable dt = ProjecUsertDAL.GetPartTimeReward_WFLog(where);
      //建立行
        HSSFRow row2 = sheet.GetRow(3);
        if (row2 == null)
            row2 = new HSSFRow(wb, sheet, 3);
        if (row2 != null)
        {
            string pid = Request["pid"];
            if (pid != null)
            {
    //擷取儲存格
                HSSFCell cell2 = row2.GetCell(1);
                cell2.SetCellValue(ProjectNO);
                HSSFCell cell6 = row2.GetCell(6);
                cell6.SetCellValue(ProjectName);
                HSSFCell cell9 = row2.GetCell(9);
                cell9.SetCellValue(date);
                HSSFCell cell12 = row2.GetCell(12);
                cell12.SetCellValue(Request["city"]);
        
        #region 輸出Excel下載
        wb.Write(Response.OutputStream);
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName("按天計薪模板.xls"), System.Text.Encoding.UTF8));
        Response.ContentType = "application/octet-stream";
        Response.Flush();
        Response.End();
        #endregion

匯入:
          // 擷取匯入的路徑
           string filePath = Path.Combine(Request.PhysicalApplicationPath, string.Format("Temp\\{0}", FileUpload1.FileName));
                Stream s = File.Create(filePath);
                s.Write(FileUpload1.FileBytes, 0, FileUpload1.FileBytes.Length);
             // 建立Excel
                HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook(s);
                HSSFSheet sheet = wb.GetSheetAt(0);
                string strAdd =string.Empty;
                string strnull = string.Empty;
                int rowCount = sheet.LastRowNum -1;
                StringBuilder sbError = new StringBuilder();
                for (int i = 2; i < rowCount; i++)
                {
                    HSSFRow row = sheet.GetRow(i);
                     strnull = CheckValid(row);

                    if (string.IsNullOrEmpty(strnull) == false)
                    {
                        sbError.Append( string.Format("<tr><td style=’cursor:hand;’ onclick=\"document.all.r{0}.style.display =document.all.r{0}.style.display == ’’?’none’:’’;\">第{0}行匯入失敗!<div id=’r{0}’ style=’display:none’>原因為:{1}</div></td></tr>"
                            ,i+1 , strnull ));
                        continue;
                    }
                    else
                    {
                         strAdd = AddData(row);
                        if (string.IsNullOrEmpty(strAdd) == false)
                        {
                            sbError.Append(string.Format("<tr><td style=’cursor:hand;’ onclick=\"document.all.r{0}.style.display =document.all.r{0}.style.display == ’’?’none’:’’;\">第{0}行匯入失敗!<div id=’r{0}’ style=’display:none’>原因為:{1}</div></td></tr>"
                             , i + 1, strAdd));

                        }

                    }
                    strAdd = string.Empty;
                    strnull = string.Empty;
                }
               
                s.Close();
                File.Delete(filePath);
                if (sbError.Length < 1)
                {
                    labMsg.Text = "匯入成功!";
                }
                else
                {
                    labMsg.Text = string.Format("<table class=’errorMsg’>{0}</table>",sbError.ToString());
                }
               控制項:http://www.openso.net/downtool?id=264

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.