ZipOutputStream 用法 小计
生活随笔
收集整理的這篇文章主要介紹了
ZipOutputStream 用法 小计
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));??
構(gòu)造函數(shù)之后?
文件就已經(jīng)創(chuàng)建出來(lái)了 只是 0kb
?
?
?
s.Write(buffer, 0, buffer.Length);? ? 內(nèi)容寫進(jìn)去了? 寫到服務(wù)器里面了? 還設(shè)置了密碼?
?
?
?最后 return 出來(lái)?
?
服務(wù)器上還是有
?
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 是流內(nèi)容 內(nèi)存的流 已寫入過(guò)內(nèi)容string strpath = Server.MapPath("/Upload/Combination/" + fileName); // 保存路徑Common.ZipUtil.ZipFileMain(ms.ToArray(), strpath, pass, outfileName); // 保存位置名字(zip) 密碼 zip中內(nèi)容文件名稱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)); // 構(gòu)造出來(lái) zip就創(chuàng)建好了 0kbs.SetLevel(6); // 0 - store only to 9 - means best compressions.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 內(nèi)容名稱entry.DateTime = DateTime.Now;entry.Size = buffer.Length; // 內(nèi)容大小s.PutNextEntry(entry);//編寫新的ZIP文件條目s.Write(buffer, 0, buffer.Length); //寫出 這句就寫出來(lái)了 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", "");//設(shè)置新密碼//NPOIHelper.ExcelPasswork(workbook, _ISysProjectService.GetProjectNameById(_userInfo.ProjectId)); workbook.WriteProtectWorkbook("", "");//設(shè)置密碼 }HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));int rowheight = 25;int colheight = 25 * 256;#region 基礎(chǔ)數(shù)據(jù)定義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頁(yè)的表頭定義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 定義表內(nèi)容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#endregionSystem.IO.MemoryStream ms = new System.IO.MemoryStream();workbook.Write(ms);return ms;}?
總結(jié)
以上是生活随笔為你收集整理的ZipOutputStream 用法 小计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 谁是颠覆者?最全盘点25条区块链主链
- 下一篇: NetworkX系列教程(1)-创建gr