c# 操作excel 替代方案

  一直使用excel com 接口进行excel 操作,最近一次因为权限折腾了个够呛,果断放弃,使用 NPOI

FileStream file = new FileStream(url, FileMode.Open, FileAccess.Read);

            HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
            HSSFSheet sheet1 = hssfworkbook.GetSheet("Sheet1") as HSSFSheet;
            sheet1.GetRow(1).Cells[5].SetCellValue (Convert.ToDateTime(Date1).ToString("yyyy年MM月dd日") + "-" + Convert.ToDateTime(Date2).ToString("yyyy年MM月dd日"));
            sheet1.ForceFormulaRecalculation = true;
            string filePath=context.Server.MapPath("~/Resource/books/" + RuleTypeID + ".xls");
            FileStream newfile = new FileStream(filePath, FileMode.Create);
 hssfworkbook.Write(newfile);
            file.Close();
            newfile.Close();
string fileName = RuleTypeID + ".xls";//客户端保存的文件名
FileInfo fileInfo = new FileInfo(filePath);
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.AddHeader("Content-Transfer-Encoding", "binary");
context.Response.ContentType = "application/octet-stream";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
context.Response.WriteFile(fileInfo.FullName);
context.Response.Flush();
context.Response.End();

 

 http://npoi.codeplex.com

 

posted on 2013-08-02 17:36  gwazy  阅读(575)  评论(0编辑  收藏  举报

导航