ZipOutputStream 用法 小计
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
构造函数之后
文件就已经创建出来了 只是 0kb
s.Write(buffer, 0, buffer.Length); 内容写进去了 写到服务器里面了 还设置了密码
最后 return 出来
服务器上还是有
public FileResult ExcelPrint(string bgrq, string endrq, string jijubianhao, string customerName, string status, string numMin, string numMax) { var user = _isysUserService.GetById(_userInfo.UserId); int? _numMin = string.IsNullOrEmpty(numMin) ? null : (int?)int.Parse(numMin); int? _numMax = string.IsNullOrEmpty(numMax) ? null : (int?)int.Parse(numMax); DateTime? _BgRq = string.IsNullOrEmpty(bgrq) ? null : (DateTime?)Convert.ToDateTime(bgrq); DateTime? _EndRq = string.IsNullOrEmpty(endrq) ? null : (DateTime?)Convert.ToDateTime(endrq); int pagecnt, thresholdCount; List<VMSelectItem> list = _IAtmAtmService.GetDataList(_BgRq, _EndRq, jijubianhao, customerName, status, 0, 0, out pagecnt, "", "", out thresholdCount, _numMin, _numMax, "export"); string[] titles = { "Atm ID", "Location Name", "Customer", "Old Password", "Current Password", "Change By", "Change Time" }; string[] props = { "One", "BeiZhu", "Remark", "Three", "Two", "UserName", "banci" }; List<PorpTitle> ptlist = new List<PorpTitle>(); string _BranchCode = _ISysProjectService.GetProjectNameById(_userInfo.ProjectId); string title = "ATM Combination Code-" + _BranchCode + " Export by: " + user.UserName; DateTime dt = DateTime.Now; string dateTime = dt.ToString("yyMMdd");//yyMMddHHmmssfff string dateTimess = dt.ToString("yyMMddHHmmssfff");//yyMMddHHmmssfff //MemoryStream ms = NpoiToExcel.ListToExcel(list, title, titles, props, ptlist); //ms.Seek(0, SeekOrigin.Begin); //string fileName = "ATM Combination Code_" + _BranchCode + dateTime + ".xls"; //return File(ms, "application/vnd.ms-excel", fileName); string pass = "COMBI00"; var syspro = _ISysProjectService.GetById(_userInfo.ProjectId); pass = syspro == null ? pass : "COMBI" + syspro.Code; string outfileName = "ATM Combination Code_" + _BranchCode + dateTime + ".xls"; string fileName = "ATM Combination Code_" + _BranchCode + dateTimess + ".zip"; string fileNameout = "ATM Combination Code_" + _BranchCode + dateTime + ".zip"; MemoryStream ms = this.ListToExcel2("CC.xls", list, title, titles, props, ptlist); // ms 是流内容 内存的流 已写入过内容 string strpath = Server.MapPath("/Upload/Combination/" + fileName); // 保存路径 Common.ZipUtil.ZipFileMain(ms.ToArray(), strpath, pass, outfileName); // 保存位置名字(zip) 密码 zip中内容文件名称 return File(strpath, "application/octet-stream", fileNameout); // 返回 }
public static void ZipFileMain(byte[] buffer, string ZipedFile, string password,string outname) { ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); // 构造出来 zip就创建好了 0kb s.SetLevel(6); // 0 - store only to 9 - means best compression s.Password = password; // 密码 //打开压缩文件 //FileStream fs = File.OpenRead(FileToZip); //byte[] buffer = new byte[fs.Length]; //fs.Read(buffer, 0, buffer.Length); //Array arr = FileToZip.Split('\\'); //string le = arr.GetValue(arr.Length - 1).ToString(); ZipEntry entry = new ZipEntry(outname); // outname 内容名称 entry.DateTime = DateTime.Now; entry.Size = buffer.Length; // 内容大小 s.PutNextEntry(entry);//编写新的ZIP文件条目 s.Write(buffer, 0, buffer.Length); //写出 这句就写出来了 s.Finish(); s.Close(); //Stream output = (Stream)s; //return output; }
public MemoryStream ListToExcel2<T>(string tmpnm, List<T> data, string strHeaderText, string[] titles, string[] props = null, List<PorpTitle> ptlist = null) { //// string configurl = System.Configuration.ConfigurationSettings.AppSettings["TempletsPath"].ToString(); string FileFullPath = Server.MapPath(configurl) + tmpnm; if (!System.IO.File.Exists(FileFullPath)) { return null; } HSSFWorkbook workbook = new HSSFWorkbook(); string pass = "COMBI00"; var syspro = _ISysProjectService.GetById(_userInfo.ProjectId); pass = syspro == null ? pass : "COMBI" + syspro.Code; using (FileStream file = new FileStream(FileFullPath, FileMode.Open, FileAccess.Read)) { //NPOIHelper.ExcelPasswork(workbook, _ISysProjectService.GetProjectNameById(_userInfo.ProjectId)); NPOI.HSSF.Record.Crypto.Biff8EncryptionKey.CurrentUserPassword = "123"; workbook = (HSSFWorkbook)WorkbookFactory.Create(file); //workbook.WriteProtectWorkbook("123456", "");//设置新密码 //NPOIHelper.ExcelPasswork(workbook, _ISysProjectService.GetProjectNameById(_userInfo.ProjectId)); workbook.WriteProtectWorkbook("", "");//设置密码 } HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); int rowheight = 25; int colheight = 25 * 256; #region 基础数据定义 HSSFCellStyle cellstyle = (HSSFCellStyle)workbook.CreateCellStyle(); cellstyle.BorderBottom = (BorderStyle)CellBorderType.THIN; cellstyle.BorderLeft = (BorderStyle)CellBorderType.THIN; cellstyle.BorderRight = (BorderStyle)CellBorderType.THIN; cellstyle.BorderTop = (BorderStyle)CellBorderType.THIN; cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; // 居中 cellstyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; #endregion #region 首行首列定义 HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0); //HSSFRow headerRow = (HSSFRow)workbook.GetSheetAt(0); headerRow.HeightInPoints = rowheight;//行高 if (strHeaderText.Contains("ATM Combination Code")) { headerRow.CreateCell(0).SetCellValue("Export Date:" + DateTime.Today.Date.ToString("MM/dd/yyyy") + " " + strHeaderText); } else { headerRow.CreateCell(0).SetCellValue(strHeaderText); } HSSFCellStyle hvstyleh = (HSSFCellStyle)workbook.CreateCellStyle(); hvstyleh.BorderBottom = (BorderStyle)CellBorderType.THIN; hvstyleh.BorderLeft = (BorderStyle)CellBorderType.THIN; hvstyleh.BorderRight = (BorderStyle)CellBorderType.THIN; hvstyleh.BorderTop = (BorderStyle)CellBorderType.THIN; hvstyleh.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER; // 居中 hvstyleh.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; // 居中 hvstyleh.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER; HSSFFont fonth = (HSSFFont)workbook.CreateFont(); fonth.FontHeightInPoints = 20; fonth.Boldweight = 700; hvstyleh.SetFont(fonth); CellRangeAddress m_region = new CellRangeAddress(0, 1, 0, titles.Count() - 1); //合并0列的n--n+2行 sheet.AddMergedRegion(m_region); setcelltag(m_region, hvstyleh, sheet); headerRow.GetCell(0).CellStyle = hvstyleh; var day = DateTime.Now; int rowIndex = 2; #endregion #region sheet页的表头定义 if (ptlist != null) { if (ptlist.Count > 0) { rowIndex = 3; HSSFRow ttdataRow = (HSSFRow)sheet.CreateRow(rowIndex - 1); ttdataRow.HeightInPoints = rowheight;//行高 foreach (PorpTitle title in ptlist) { HSSFCell ttCell = (HSSFCell)ttdataRow.CreateCell(title.Poss); ttCell.SetCellValue(title.TitleNM); if (title.Poss != title.Pose) { CellRangeAddress m_region1 = new CellRangeAddress(2, 2, title.Poss, title.Pose); //合并0列的n--n+2行 sheet.AddMergedRegion(m_region1); setcelltag(m_region1, cellstyle, sheet); ttCell.CellStyle = cellstyle; } else { ttCell.CellStyle = cellstyle; } } } } #endregion #region 表记录记录 var jyzindex = rowIndex; #region 定义表头 int cellcnt = 0; HSSFRow dataRow = (HSSFRow)sheet.CreateRow(jyzindex); dataRow.HeightInPoints = rowheight;//行高 HSSFCell newCell; foreach (string prop in titles) { newCell = (HSSFCell)dataRow.CreateCell(cellcnt); newCell.SetCellValue(prop); newCell.CellStyle = cellstyle; sheet.SetColumnWidth(cellcnt, colheight); cellcnt++; } #endregion #region 定义表内容 jyzindex++; foreach (T item in data) { dataRow = (HSSFRow)sheet.CreateRow(jyzindex); dataRow.HeightInPoints = rowheight;//行高 cellcnt = 0; if (props != null) { foreach (string prop in props) { PropertyDescriptor prop1 = properties[prop]; var val = ""; try { val = ConvertHelper.ToStr(prop1.GetValue(item) ?? DBNull.Value); } catch { } newCell = (HSSFCell)dataRow.CreateCell(cellcnt); newCell.SetCellValue(val); newCell.CellStyle = cellstyle; sheet.SetColumnWidth(cellcnt, colheight); cellcnt++; } } else { foreach (PropertyDescriptor prop in properties) { var val = ""; try { val = ConvertHelper.ToStr(prop.GetValue(item) ?? DBNull.Value); } catch { } newCell = (HSSFCell)dataRow.CreateCell(cellcnt); newCell.SetCellValue(val); newCell.CellStyle = cellstyle; sheet.SetColumnWidth(cellcnt, colheight); cellcnt++; } } jyzindex++; } //sheet.ProtectSheet("password"); #endregion #endregion System.IO.MemoryStream ms = new System.IO.MemoryStream(); workbook.Write(ms); return ms; }