| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401 |
-
- using System;
- using System.Data;
- using System.IO;
- using System.Text;
- using System.Web;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- using NPOI.XSSF.UserModel;
- using CallCenterApi.DB;
- using System.Collections.Generic;
- namespace CallCenter.Utility
- {
- public class NPOIHelper
- {
- private string _title;
- private string _sheetName;
- private string _filePath;
- /// <summary>
- /// 导出到Excel
- /// </summary>
- /// <param name="table"></param>
- /// <returns></returns>
- public bool ToExcel(DataTable table, string[] columns = null)
- {
- FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- IWorkbook workBook = new HSSFWorkbook();
- if (string.IsNullOrWhiteSpace(this._sheetName))
- {
- this._sheetName = "sheet1";
- }
- ISheet sheet = workBook.CreateSheet(this._sheetName);
- //处理表格标题
- IRow row = sheet.CreateRow(0);
- row.CreateCell(0).SetCellValue(this._title);
- sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
- row.Height = 500;
- ICellStyle cellStyle = workBook.CreateCellStyle();
- IFont font = workBook.CreateFont();
- font.FontName = "微软雅黑";
- font.FontHeightInPoints = 17;
- cellStyle.SetFont(font);
- cellStyle.VerticalAlignment = VerticalAlignment.Center;
- cellStyle.Alignment = HorizontalAlignment.Center;
- row.Cells[0].CellStyle = cellStyle;
- //处理表格列头
- row = sheet.CreateRow(1);
- if (columns == null)
- {
- for (int i = 0; i < table.Columns.Count; i++)
- {
- row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
- row.Height = 350;
- sheet.AutoSizeColumn(i);
- }
- }
- else
- {
- for (int i = 0; i < columns.Length; i++)
- {
- row.CreateCell(i).SetCellValue(columns[i]);
- row.Height = 350;
- sheet.AutoSizeColumn(i);
- }
- }
- //处理数据内容
- for (int i = 0; i < table.Rows.Count; i++)
- {
- row = sheet.CreateRow(2 + i);
- row.Height = 250;
- for (int j = 0; j < table.Columns.Count; j++)
- {
- row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
- sheet.SetColumnWidth(j, 256 * 15);
- }
- }
- //写入数据流
- workBook.Write(fs);
- fs.Flush();
- fs.Close();
- return true;
- }
- /// <summary>
- /// 导出到Excel
- /// </summary>
- /// <param name="table"></param>
- /// <param name="title"></param>
- /// <param name="sheetName">空字符串或null的话默认sheet1</param>
- /// <param name="columns">自定义表格列头,默认null</param>
- /// <returns></returns>
- public bool ToExcel(DataTable table, string title, string sheetName, string filePath, string[] columns = null)
- {
- this._title = title;
- this._sheetName = sheetName;
- this._filePath = filePath;
- return ToExcel(table, columns);
- }
- /// <summary>
- /// 弹出下载框导出excel
- /// </summary>
- /// <param name="Name"></param>
- /// <param name="dt"></param>
- /// <returns></returns>
- public string ExportToExcel(string Name, DataTable dt, string[] cols = null,int iswork=0)
- {
- try
- {
- //if (dt.Rows.Count > 0)
- //{
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle HeadercellStyle = workbook.CreateCellStyle();
- HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.Alignment = HorizontalAlignment.Center;
- HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- HeadercellStyle.FillPattern = FillPattern.SolidForeground;
- HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- //字体
-
- if (iswork>0)
- {
- IDataFormat format = workbook.CreateDataFormat();
- HeadercellStyle.DataFormat = format.GetFormat("[DbNum2][$-804]0");
- }
- NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
- headerfont.Boldweight = (short)FontBoldWeight.Bold;
- headerfont.FontHeightInPoints = 12;
- HeadercellStyle.SetFont(headerfont);
- //用column name 作为列名
- int icolIndex = 0;
- IRow headerRow = sheet.CreateRow(0);
- if (cols == null || (cols != null && cols.Length == 0))
- {
- foreach (DataColumn dc in dt.Columns)
- {
- ICell cell = headerRow.CreateCell(icolIndex);
- cell.SetCellValue(dc.ColumnName);
- cell.CellStyle = HeadercellStyle;
- icolIndex++;
- }
- }
- else
- {
- foreach (string dc in cols)
- {
- ICell cell = headerRow.CreateCell(icolIndex);
- cell.SetCellValue(dc);
- cell.CellStyle = HeadercellStyle;
- icolIndex++;
- }
- }
- ICellStyle cellStyle = workbook.CreateCellStyle();
- //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
- cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
- cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
- //建立内容行
- int iRowIndex = 0;
- foreach (DataRow dr in dt.Rows)
- {
- int iCellIndex = 0;
- IRow irow = sheet.CreateRow(iRowIndex + 1);
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- string strsj = string.Empty;
- if (dr[i] != null)
- {
- strsj = dr[i].ToString();
- }
- ICell cell = irow.CreateCell(iCellIndex);
- cell.SetCellValue(strsj);
- cell.CellStyle = cellStyle;
- iCellIndex++;
- }
- iRowIndex++;
- }
- //自适应列宽度
- for (int i = 0; i < icolIndex; i++)
- {
- sheet.AutoSizeColumn(i);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
- //}
- return "";
- }
- catch
- {
- return "导出失败!";
- }
- }
- /// <summary>
- /// 合并单元格
- /// </summary>
- /// <param name="sheet">要合并单元格所在的sheet</param>
- /// <param name="rowstart">开始行的索引</param>
- /// <param name="rowend">结束行的索引</param>
- /// <param name="colstart">开始列的索引</param>
- /// <param name="colend">结束列的索引</param>
- public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
- {
- CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
- sheet.AddMergedRegion(cellRangeAddress);
- }
- /// <summary>
- /// 工单类型弹出下载框导出excel
- /// </summary>
- /// <param name="Name"></param>
- /// <param name="dt"></param>
- /// <param name="typeclass">仪器或试剂</param>
- /// <returns></returns>
- public string ExportToExcelForGDLX(string Name, DataTable dt, string typeclass, List<string> colnames, List<int> erows, List<string> secolnames)
- {
- try
- {
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle HeadercellStyle = workbook.CreateCellStyle();
- HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- HeadercellStyle.Alignment = HorizontalAlignment.Center;
- HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- HeadercellStyle.FillPattern = FillPattern.SolidForeground;
- HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
- //字体
- NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
- headerfont.Boldweight = (short)FontBoldWeight.Bold;
- headerfont.FontHeightInPoints = 12;
- HeadercellStyle.SetFont(headerfont);
- //用column name 作为列名
- int icolIndex = 0;
- IRow headerRow = sheet.CreateRow(0);
-
- for (int i = 0; i < colnames.Count; i++)
- {
- ICell cell = headerRow.CreateCell(i);
- cell.SetCellValue(colnames[i]);
- cell.CellStyle = HeadercellStyle;
- //SetCellRangeAddress(sheet, 0, 0, erows[i * 2], erows[i * 2 + 1]);
- }
-
- for (int k = 0; k < erows.Count / 2; k++)
- {
- SetCellRangeAddress(sheet, 0, 0, erows[k * 2], erows[k * 2 + 1]);
- }
- //添加第二行标题
- IRow SecRow = sheet.CreateRow(1);
- for (int i = 0; i < secolnames.Count; i++)
- {
- ICell cell = SecRow.CreateCell(i);
- cell.SetCellValue(secolnames[i].ToString());
- }
- ICellStyle cellStyle = workbook.CreateCellStyle();
- //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
- cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
- cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
- //建立内容行
- int iRowIndex = 0;
- foreach (DataRow dr in dt.Rows)
- {
- int iCellIndex = 0;
- IRow irow = sheet.CreateRow(iRowIndex + 2);
- for (int i = 0; i < dt.Columns.Count; i++)
- {
- string strsj = string.Empty;
- if (dr[i] != null)
- {
- strsj = dr[i].ToString();
- }
- ICell cell = irow.CreateCell(iCellIndex);
- cell.SetCellValue(strsj);
- cell.CellStyle = cellStyle;
- iCellIndex++;
- }
- iRowIndex++;
- }
- //自适应列宽度
- for (int i = 0; i < icolIndex; i++)
- {
- sheet.AutoSizeColumn(i);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
-
- return "";
- }
- catch (Exception e)
- {
- return "导出失败!" + e.Message;
- }
- }
- /// <summary>
- /// 导入excel转换为datatable
- /// </summary>
- /// <param name="upfile"></param>
- /// <param name="headrow"></param>
- /// <returns></returns>
- public DataTable ExcelToTable(HttpPostedFile upfile, int headrow)
- {
- DataTable dt = new DataTable();
- IWorkbook workbook = null;
- Stream stream = upfile.InputStream;
- string suffix = upfile.FileName.Substring(upfile.FileName.LastIndexOf(".") + 1).ToLower();
- if (suffix == "xlsx") // 2007版本
- {
- workbook = new XSSFWorkbook(stream);
- }
- else if (suffix == "xls") // 2003版本
- {
- workbook = new HSSFWorkbook(stream);
- }
- //获取excel的第一个sheet
- ISheet sheet = workbook.GetSheetAt(0);
- //获取sheet的第一行
- IRow headerRow = sheet.GetRow(headrow);
- //一行最后一个方格的编号 即总的列数
- int cellCount = headerRow.LastCellNum;
- //最后一列的标号 即总的行数
- int rowCount = sheet.LastRowNum;
- //列名
- for (int i = 0; i < cellCount; i++)
- {
- dt.Columns.Add(headerRow.GetCell(i).ToString());
- }
- for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
- {
- DataRow dr = dt.NewRow();
- IRow row = sheet.GetRow(i);
- for (int j = row.FirstCellNum; j < cellCount; j++)
- {
- if (row.GetCell(j) != null)
- {
- dr[j] = row.GetCell(j).ToString();
- }
- }
- dt.Rows.Add(dr);
- }
- sheet = null;
- workbook = null;
- return dt;
- }
- /// <summary>
- /// 导入excel转换为datatable
- /// </summary>
- /// <param name="upfile"></param>
- /// <param name="headrow"></param>
- /// <returns></returns>
- public DataTable ExcelToTable(string fileName, int headrow)
- {
- DataTable dt = new DataTable();
- IWorkbook workbook = null;
- Stream stream = new FileStream(fileName, FileMode.OpenOrCreate);
- string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
- if (suffix == "xlsx") // 2007版本
- {
- workbook = new XSSFWorkbook(stream);
- }
- else if (suffix == "xls") // 2003版本
- {
- workbook = new HSSFWorkbook(stream);
- }
- //获取excel的第一个sheet
- ISheet sheet = workbook.GetSheetAt(0);
- //获取sheet的第一行
- IRow headerRow = sheet.GetRow(headrow);
- //一行最后一个方格的编号 即总的列数
- int cellCount = headerRow.LastCellNum;
- //最后一列的标号 即总的行数
- int rowCount = sheet.LastRowNum;
- //列名
- for (int i = 0; i < cellCount; i++)
- {
- dt.Columns.Add(headerRow.GetCell(i).ToString());
- }
- for (int i = (sheet.FirstRowNum + headrow + 1); i <= sheet.LastRowNum; i++)
- {
- DataRow dr = dt.NewRow();
- IRow row = sheet.GetRow(i);
- for (int j = row.FirstCellNum; j < cellCount; j++)
- {
- if (row.GetCell(j) != null)
- {
- dr[j] = row.GetCell(j).ToString();
- }
- }
- dt.Rows.Add(dr);
- }
- sheet = null;
- workbook = null;
- return dt;
- }
- /// <summary>
- /// 简报导出
- /// </summary>
- /// <param name="ds"></param>
- /// <returns></returns>
- public string MarketExportToExcel(string stime,string etime,Market market )
- {
- try
- {
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle cellStyle = workbook.CreateCellStyle();
- //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
- //cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
- //cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- //cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- //cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- //cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
- ICellStyle cellStylebt = workbook.CreateCellStyle();
- NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
- cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
- cellStylebt.SetFont(cellfontbt);
- cellStylebt.VerticalAlignment = VerticalAlignment.Center;
- cellStylebt.Alignment = HorizontalAlignment.Center;
- IRow irow1 = sheet.CreateRow(1);
- ICell cell1 = irow1.CreateCell(0);
- cell1.SetCellValue("时间:" + stime + " 至 " + etime);
- cell1.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7));
- //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(new CellRangeAddress(1, 1, 0, 7), BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
- #region 产品投诉情况
- List<Ification> dt1 = market.product ;
- IRow irow2 = sheet.CreateRow(2);
- ICell cell2 = irow2.CreateCell(0);
- cell2.SetCellValue("产品投诉情况");
- cell2.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 7));
- int dtrow = 3;
- if (dt1.Count >0)
- {
- int dt = 0;
- int a = dt1.Count / 3;
- float b = dt1.Count % 3;
- if (b >0)
- a = a + 1;
- dtrow += a;
- List<IRow> irow3list = new List<IRow>();
- for (int i =0;i <a;i++)
- {
- IRow irow3 = sheet.CreateRow(3 + i);
- irow3list.Add(irow3);
- }
- int td = 0;
- foreach (var it in dt1)
- {
- dt++;
- if (dt <4)
- {
- ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
- cell101.SetCellValue(it.name );
- cell101.CellStyle = cellStylebt;
- ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
- cell102.SetCellValue(it .number );
- cell102.CellStyle = cellStylebt;
- ICell cell103= irow3list[td].CreateCell(3 * dt-1 );
- cell103.SetCellValue(it.Proportion );
- cell103.CellStyle = cellStylebt;
- }
- else
- {
- dt = 1;
- td++;
- ICell cell101 = irow3list[td].CreateCell(3 * dt - 3);
- cell101.SetCellValue(it.name);
- cell101.CellStyle = cellStylebt;
- ICell cell102 = irow3list[td].CreateCell(3 * dt - 2);
- cell102.SetCellValue(it.number);
- cell102.CellStyle = cellStylebt;
- ICell cell103 = irow3list[td].CreateCell(3 * dt-1);
- cell103.SetCellValue(it.Proportion);
- cell103.CellStyle = cellStylebt;
- }
- }
-
- }
- #endregion
- #region 服务投诉情况
- Ification dt2 = market.service;
- dtrow++;
- IRow irow4 = sheet.CreateRow(dtrow);
- ICell cell4 = irow4.CreateCell(0);
- cell4.SetCellValue("服务投诉情况");
- cell4.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt2!=null )
- {
- dtrow++;
- IRow irow5 = sheet.CreateRow(dtrow);
- ICell cell51 = irow5.CreateCell(0);
- cell51.SetCellValue(dt2.name);
- cell51.CellStyle = cellStylebt;
- ICell cell52 = irow5.CreateCell(1);
- cell52.SetCellValue(dt2.number);
- cell52.CellStyle = cellStylebt;
- ICell cell53 = irow5.CreateCell(2);
- cell53.SetCellValue(dt2.Proportion);
- cell53.CellStyle = cellStylebt;
- }
-
- #endregion
- #region 涉媒投诉
- Ification dt3 = market.sediainvolved;
- dtrow++;
- IRow irow9 = sheet.CreateRow(dtrow);
- ICell cell9 = irow9.CreateCell(0);
- cell9.SetCellValue("涉媒投诉");
- cell9.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt3!=null )
- {
- dtrow++;
- IRow irow10 = sheet.CreateRow(dtrow);
- ICell cell53 = irow10.CreateCell(0);
- cell53.SetCellValue(dt3.name);
- cell53.CellStyle = cellStylebt;
- ICell cell54 = irow10.CreateCell(1);
- cell54.SetCellValue(dt3.number);
- cell54.CellStyle = cellStylebt;
- ICell cell55 = irow10.CreateCell(2);
- cell55.SetCellValue(dt3.Proportion);
- cell55.CellStyle = cellStylebt;
- }
-
- #endregion
- #region 市场抽检
- Ification dt4 = market.spotcheck;
- dtrow++;
- IRow irow11 = sheet.CreateRow(dtrow);
- ICell cell12 = irow11.CreateCell(0);
- cell12.SetCellValue("市场抽检");
- cell12.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt4!=null )
- {
- dtrow++;
- IRow irow12 = sheet.CreateRow(dtrow);
- ICell cell55 = irow12.CreateCell(0);
- cell55.SetCellValue(dt4.name);
- cell55.CellStyle = cellStylebt;
- ICell cell56 = irow12.CreateCell(1);
- cell56.SetCellValue(dt4.number);
- cell56.CellStyle = cellStylebt;
- ICell cell57 = irow12.CreateCell(2);
- cell57.SetCellValue(dt4.Proportion);
- cell57.CellStyle = cellStylebt;
- }
-
- #endregion
- #region 其他信息
- Ification dt5 = market.other;
- dtrow++;
- IRow irow13 = sheet.CreateRow(dtrow);
- ICell cell13 = irow13.CreateCell(0);
- cell13.SetCellValue("其他信息");
- cell13.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(dtrow, dtrow, 0, 7));
- if (dt5!=null )
- {
- dtrow++;
- IRow irow15 = sheet.CreateRow(dtrow);
- ICell cell57 = irow15.CreateCell(0);
- cell57.SetCellValue(dt4.name);
- cell57.CellStyle = cellStylebt;
- ICell cell58 = irow15.CreateCell(1);
- cell58.SetCellValue(dt4.number);
- cell58.CellStyle = cellStylebt;
- ICell cell59 = irow15.CreateCell(2);
- cell59.SetCellValue(dt5.Proportion);
- cell59.CellStyle = cellStylebt;
- }
-
- #endregion
- //自适应列宽度
- for (int i = 0; i < 8; i++)
- {
- // sheet.AutoSizeColumn(i);
- sheet.SetColumnWidth(i, 12 * 256);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode("市场信息简报" + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
- return "";
- }
- catch
- {
- return "导出失败!";
- }
- }
- private void Columnwidth(int number, ISheet ffSheet)
- {
- for (int columnNum = 0; columnNum <= number; columnNum++)
- {
- int columnWidth = ffSheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度
- for (int rowNum = 1; rowNum <= ffSheet.LastRowNum; rowNum++)//在这一列上循环行
- {
- IRow currentRow = ffSheet.GetRow(rowNum);
- ICell currentCell = currentRow.GetCell(columnNum);
- int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取当前单元格的内容宽度
- if (columnWidth < length + 1)
- {
- columnWidth = length + 1;
- }//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度,后面的+1是我人为的将宽度增加一个字符
- }
- }
- }
- private string ReturnMsg(int j, int i, List<Factory> Factory1)
- {
- string msg = "";
- switch (j)
- {
- case 0:
- msg = Factory1[i * 2].name;
- break;
- case 1:
- msg = Factory1[i * 2].number.ToString();
- break;
- case 2:
- msg = Factory1[i * 2].proportion;
- break;
- case 3:
- msg = Factory1[i * 2 + 1].name;
- break;
- case 4:
- msg = Factory1[i * 2 + 1].number.ToString();
- break;
- case 5:
- msg = Factory1[i * 2 + 1].proportion;
- break;
- }
- return msg;
- }
- /// <summary>
- /// 投诉产品日期分布表
- /// </summary>
- /// <param name="ds"></param>
- /// <returns></returns>
- public string DistributionToExcel(DateTime datetime, Product product)
- {
- try
- {
- HSSFWorkbook workbook = new HSSFWorkbook();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- ICellStyle cellStyle = workbook.CreateCellStyle();
-
- NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
- cellfont.Boldweight = (short)FontBoldWeight.Normal;
- cellStyle.SetFont(cellfont);
-
- ICellStyle cellStylebt = workbook.CreateCellStyle();
- cellStylebt.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStylebt.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStylebt.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStylebt.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- NPOI.SS.UserModel.IFont cellfontbt = workbook.CreateFont();
- cellfontbt.Boldweight = (short)FontBoldWeight.Bold;
-
- cellStylebt.SetFont(cellfontbt);
- cellStylebt.VerticalAlignment = VerticalAlignment.Center;
- cellStylebt.Alignment = HorizontalAlignment.Center;
- IRow irow1 = sheet.CreateRow(0);
- ICell cell1 = irow1.CreateCell(0);
- cell1.SetCellValue(datetime.Year +"年"+datetime .Month +"月份投诉产品日期分布情况表");
- cell1.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 13));
- #region 产品投诉情况
- ICellStyle cellStylebt1 = workbook.CreateCellStyle();
- cellStylebt1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- cellStylebt1.SetFont(cellfontbt);
- cellStylebt1.VerticalAlignment = VerticalAlignment.Center;
- cellStylebt1.Alignment = HorizontalAlignment.Center;
- IRow irow2 = sheet.CreateRow(1);
- ICell cell2 = irow2.CreateCell(0);
- ICellStyle style13 = workbook.CreateCellStyle();
- style13.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- style13.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- style13.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- style13.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- style13.BorderDiagonalLineStyle = BorderStyle.Thin;
- style13.BorderDiagonal = BorderDiagonal.Backward;
- style13.BorderDiagonalColor = IndexedColors.Black .Index;
- string sb = " 数量\n日期";
- // StringBuilder sb = new StringBuilder();
- // sb.AppendLine("数量".PadLeft(15));//该行不足长度10在左侧填空格
- // sb.AppendLine("日期".PadRight(15));//该行不足长度10在右侧填空格
- cell2.SetCellValue(sb);
- cell2.CellStyle = cellStylebt;
- style13.WrapText = true;
- irow2.GetCell(0).CellStyle = style13;
- sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0,0 ));
- ICell cell6 = irow2.CreateCell(1);
- cell6.SetCellValue("投诉产品");
- cell6.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 1));
- for (int i=0;i<5;i ++)
- {
- string msg = "";
- switch (i)
- {
- case 0:
- msg = datetime.AddMonths(-11).Month + "-" + datetime.AddMonths(-3).Month + "月份日期";
- break;
- case 1:
- msg = datetime.AddMonths(-2).Month + "月份日期";
- break;
- case 2:
- msg = datetime.AddMonths(-1).Month + "月份日期";
- break;
- case 3:
- msg = datetime.Month + "月份日期";
- break;
- case 4:
- msg = "不清楚日期";
- break;
- }
- ICell cell3 = irow2.CreateCell(i *2+ 2 );
- cell3.SetCellValue(msg);
- cell3.CellStyle = cellStylebt;
- ICell cell4 = irow2.CreateCell(i * 2 + 3);
- cell4.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, i * 2 + 2, i * 2 + 3));
- }
- IRow irow3 = sheet.CreateRow(2);
- for (int i = 0; i < 11; i++)
- {
- string msg = "";
- if (i ==0)
- {
- msg = "数量";
- }
- else
- {
- if (i % 2 == 0)
- {
- msg = "占比";
- }
- else
- {
- msg = "数量";
- }
- }
- ICell cell4 = irow3.CreateCell(i+1);
- cell4.SetCellValue(msg);
- cell4.CellStyle = cellStylebt;
-
- }
- List<Date> dt1 = product.dates;
- if (dt1!=null )
- {
- for (int i=0;i < dt1.Count;i ++)
- {
- IRow irow4 = sheet.CreateRow(3 + i);
- string msg = "";
- for (int j = 0; j < 12; j ++)
- {
- switch (j )
- {
- case 0:
- msg = dt1[i].name;
- break;
- case 1:
- msg = dt1[i].total.ToString();
- break;
- case 2:
- msg = dt1[i].MonthCount1 .ToString ();
- break;
- case 3:
- msg = dt1[i].MonthCountmix1 .ToString();
- break;
- case 4:
- msg = dt1[i].MonthCount2.ToString();
- break;
- case 5:
- msg = dt1[i].MonthCountmix2.ToString();
- break;
- case 6:
- msg = dt1[i].MonthCount3.ToString();
- break;
- case 7:
- msg = dt1[i].MonthCountmix3.ToString();
- break;
- case 8:
- msg = dt1[i].MonthCount4.ToString();
- break;
- case 9:
- msg = dt1[i].MonthCountmix4.ToString();
- break;
- case 10:
- msg = dt1[i].MonthCount5.ToString();
- break;
- case 11:
- msg = dt1[i].MonthCountmix5.ToString();
- break;
-
- }
- ICell cell5 = irow4.CreateCell(j);
- cell5.SetCellValue(msg);
- cell5.CellStyle = cellStylebt;
- }
- }
- }
- int t = dt1.Count + 2;int count = 0;
- List<Factory> Factory1 = product.factory;
- List<Factory> Factory2 = product.problem;
- List<Factory> Factory3 = product.product;
- int factory = 0, problem = 0, productcode = 0;
- if (Factory1 != null)
- factory = (Factory1.Count / 2) + (0 == Factory1.Count % 2 ? 0 : 1);
- if (Factory2 != null)
- problem = Factory2.Count;
- if (Factory3 != null)
- productcode = Factory3.Count;
- bool istrue = true; int a = 0, total = problem + productcode;
- if (factory>(total))
- {
- count = factory;
- a = factory - total;
- }
- else
- {
- istrue = false;
- count = total;
- }
- IRow irow5 = sheet.CreateRow(t+1);
- ICell cell7 = irow5.CreateCell(0);
- cell7.SetCellValue("工\n厂\n投\n诉\n占\n比\n");
- cell7.CellStyle = cellStylebt;
- cell7.CellStyle.WrapText=true ;
- sheet.AddMergedRegion(new CellRangeAddress(t+1, t+ count, 0, 0));
- ICell cell10 = irow5.CreateCell(7);
- cell10.SetCellValue("各\n质\n量\n问\n题\n占\n比\n");
- cell10.CellStyle = cellStylebt;
- cell10.CellStyle.WrapText = true;
- int structure = t + problem;
- if (istrue)
- {
- if (a >1)
- {
- structure = t + problem + a / 2;
- sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem + a / 2, 7, 7));
- }
-
- else
- {
- structure = t + problem;
- sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
- }
- }
- else
- sheet.AddMergedRegion(new CellRangeAddress(t+1, t + problem, 7, 7));
- IRow irow7 = sheet.CreateRow(structure+1);
- ICell cell11 = irow7.CreateCell(7);
- cell11.SetCellValue("结\n构\n占\n比\n");
- cell11.CellStyle = cellStylebt;
- cell11.CellStyle.WrapText = true;
- if (istrue)
- {
- if (a > 1)
- sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure+ productcode+a/2, 7, 7));
- else
- sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode+a, 7, 7));
- }
- else
- sheet.AddMergedRegion(new CellRangeAddress(structure + 1, structure + productcode, 7, 7));
- if (Factory1 != null)
- {
- for (int i = 0; i < count; i++)
- {
- if (i ==0 || t + i== structure)
- {
- if (Factory1.Count > i*2)
- {
- for (int j = 0; j < 6; j++)
- {
- string msg = ReturnMsg(j, i, Factory1);
- if (i == 0)
- {
- ICell cell8 = irow5.CreateCell(j + 1);
- cell8.SetCellValue(msg);
- cell8.CellStyle = cellStylebt;
- }
- else
- {
- ICell cell9 = irow7.CreateCell(j + 1);
- cell9.SetCellValue(msg);
- cell9.CellStyle = cellStylebt;
- }
- }
- }
-
- if (i ==0)
- {
- for (int z = 0; z < 3; z++)
- {
- switch (z)
- {
- case 0:
- ICell cell12 = irow5.CreateCell(8);
- cell12.SetCellValue(Factory2[0].name );
- cell12.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t+1, t+1 , 8, 9));
- ICell cell22 = irow5.CreateCell(9);
- cell22.CellStyle = cellStylebt;
- break;
- case 1:
- ICell cell13 = irow5.CreateCell(10);
- cell13.SetCellValue(Factory2[0].number .ToString ());
- cell13.CellStyle = cellStylebt;
-
- break;
- case 2:
- ICell cell14 = irow5.CreateCell(11);
- cell14.SetCellValue(Factory2[0].proportion);
- cell14.CellStyle = cellStylebt;
- break;
- }
-
- }
- }
- else
- {
- ICell cell119 = irow7.CreateCell(0);
- cell119.CellStyle = cellStylebt;
- for (int z = 0; z < 3; z++)
- {
- switch (z)
- {
- case 0:
- ICell cell15 = irow7.CreateCell(8);
- cell15.SetCellValue(Factory3[0].name);
- cell15.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(structure+1, structure+1, 8, 9));
- ICell cell31 = irow7.CreateCell(9);
- cell15.CellStyle = cellStylebt;
- break;
- case 1:
- ICell cell16 = irow7.CreateCell(10);
- cell16.SetCellValue(Factory3[0].number.ToString());
- cell16.CellStyle = cellStylebt;
- break;
- case 2:
- ICell cell17= irow7.CreateCell(11);
- cell17.SetCellValue(Factory3[0].proportion);
- cell17.CellStyle = cellStylebt;
- break;
- }
- }
- }
-
- }
- else
- {
-
- IRow irow6 = sheet.CreateRow(t+1 + i);
- if (Factory1.Count %2!=0)
- {
- if (i * 2+1 < Factory1.Count)
- {
- for (int j = 0; j < 6; j++)
- {
- string msg = ReturnMsg(j, i, Factory1);
- ICell cell18 = irow6.CreateCell(j + 1);
- cell18.SetCellValue(msg);
- cell18.CellStyle = cellStylebt;
- }
- }
- else if (i * 2 + 1 == Factory1.Count)
- {
- for (int j = 0; j < 3; j++)
- {
- string msg = ReturnMsg(j, i, Factory1);
- ICell cell18 = irow6.CreateCell(j + 1);
- cell18.SetCellValue(msg);
- cell18.CellStyle = cellStylebt;
- }
- }
- }
- else
- {
- if (i * 2 < Factory1.Count)
- {
- for (int j = 0; j < 6; j++)
- {
- string msg = ReturnMsg(j, i, Factory1);
- ICell cell18 = irow6.CreateCell(j + 1);
- cell18.SetCellValue(msg);
- cell18.CellStyle = cellStylebt;
- }
- }
- }
-
- int b = 0;
- if (a >1)
- {
- b= (a / 2) + (0 == a % 2 ? 0 : 1);
- }
- if (i < Factory2.Count -1)
- {
- for (int z = 0; z < 3; z++)
- {
- switch (z)
- {
- case 0:
- ICell cell8 = irow6.CreateCell(8);
- cell8.SetCellValue(Factory2[i].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i, 8, 9));
- ICell cell22 = irow6.CreateCell(9);
- cell22.CellStyle = cellStylebt;
- break;
- case 1:
- ICell cell9 = irow6.CreateCell(10);
- cell9.SetCellValue(Factory2[i].number.ToString());
- cell9.CellStyle = cellStylebt;
- break;
- case 2:
- ICell cell12 = irow6.CreateCell(11);
- cell12.SetCellValue(Factory2[i].proportion);
- cell12.CellStyle = cellStylebt;
- break;
- }
- }
- }
- else if (i == Factory2.Count-1 )
- {
- for (int z = 0; z < 3; z++)
- {
- string msg = "";
- switch (z)
- {
- case 0:
- ICell cell8 = irow6.CreateCell(8);
- cell8.SetCellValue(Factory2[i].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i+b , 8, 9));
- if (istrue )
- {
- ICell cell22 = irow6.CreateCell(9);
- cell22.CellStyle = cellStylebt;
- }
- break;
- case 1:
- ICell cell9 = irow6.CreateCell(10);
- cell9.SetCellValue(Factory2[i].number.ToString());
- cell9.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 10, 10));
- break;
- case 2:
- ICell cell12 = irow6.CreateCell(11);
- cell12.SetCellValue(Factory2[i].proportion);
- cell12.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t+1 + i, t+1 + i + b, 11, 11));
- break;
- }
-
- }
- }
- if (i <( Factory3.Count + Factory2.Count+b -1)&& i>= Factory2.Count + b)
- {
- for (int z = 0; z < 3; z++)
- {
-
- switch (z)
- {
- case 0:
- ICell cell8 = irow6.CreateCell(8);
- cell8.SetCellValue(Factory3[i- Factory2.Count -b ].name);
- cell8.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t +1+ i, 8, 9));
- ICell cel27 = irow6.CreateCell(9);
- cel27.CellStyle = cellStylebt;
- break;
- case 1:
- ICell cell9 = irow6.CreateCell(10);
- cell9.SetCellValue(Factory3[i - Factory2.Count - b ].number.ToString());
- cell9.CellStyle = cellStylebt;
- break;
- case 2:
- ICell cell12 = irow6.CreateCell(11);
- cell12.SetCellValue(Factory3[i - Factory2.Count - b ].proportion);
- cell12.CellStyle = cellStylebt;
- break;
- }
- }
- }
- else if ( i== Factory2.Count + b+ Factory3.Count-1 )
- {
- for (int z = 0; z < 3; z++)
- {
- switch (z)
- {
- case 0:
- ICell cell8 = irow6.CreateCell(8);
- cell8.SetCellValue(Factory3[i - Factory2.Count - b].name);
- cell8.CellStyle = cellStylebt;
-
- sheet.AddMergedRegion(new CellRangeAddress(t +1+ i, t+1 + i + b, 8, 9));
- break;
- case 1:
- ICell cell9 = irow6.CreateCell(10);
- cell9.SetCellValue(Factory3[i - Factory2.Count - b].number.ToString());
- cell9.CellStyle = cellStylebt;
-
- sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 10, 10));
- break;
- case 2:
- ICell cell12 = irow6.CreateCell(11);
- cell12.SetCellValue(Factory3[i - Factory2.Count - b].proportion);
- cell12.CellStyle = cellStylebt;
-
- sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i + b, 11, 11));
- break;
- }
- }
- }
-
- if ( i == Factory2.Count)
- {
- ICell cell120 = irow6.CreateCell(7);
- cell120.CellStyle = cellStylebt;
- ICell cell124 = irow6.CreateCell(9);
- cell124.CellStyle = cellStylebt;
- ICell cell125 = irow6.CreateCell(10);
- cell125.CellStyle = cellStylebt;
- ICell cell126 = irow6.CreateCell(11);
- cell126.CellStyle = cellStylebt;
-
- }
- if (!istrue )
- {
- if (i * 2>= Factory1.Count)
- {
- ICell cell119 = irow6.CreateCell(0);
- cell119.CellStyle = cellStylebt;
-
- }
- if (i *2 +1>= Factory1.Count&i < Factory2.Count)
- {
- ICell cell119 = irow6.CreateCell(7);
- cell119.CellStyle = cellStylebt;
- }
- if (i > Factory2.Count)
- {
- ICell cell119 = irow6.CreateCell(7);
- cell119.CellStyle = cellStylebt;
- }
- if (i == count - 1)
- {
- ICell cell119 = irow6.CreateCell(1);
- cell119.CellStyle = cellStylebt1;
- ICell cell121 = irow6.CreateCell(2);
- cell121.CellStyle = cellStylebt1;
- ICell cell132 = irow6.CreateCell(3);
- cell132.CellStyle = cellStylebt1;
- ICell cell128 = irow6.CreateCell(4);
- cell128.CellStyle = cellStylebt1;
- ICell cell129 = irow6.CreateCell(5);
- cell129.CellStyle = cellStylebt1;
- ICell cell130 = irow6.CreateCell(6);
- cell130.CellStyle = cellStylebt1;
- ICell cell131 = irow6.CreateCell(9);
- cell131.CellStyle = cellStylebt1;
- }
- }
- else if (i == count - 1)
- {
- ICell cell119 = irow6.CreateCell(0);
- cell119.CellStyle = cellStylebt;
- ICell cell121 = irow6.CreateCell(7);
- cell121.CellStyle = cellStylebt;
- ICell cell122 = irow6.CreateCell(4);
- cell122.CellStyle = cellStylebt;
- ICell cell123 = irow6.CreateCell(5);
- cell123.CellStyle = cellStylebt;
- ICell cell124 = irow6.CreateCell(6);
- cell124.CellStyle = cellStylebt;
- sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i, 4, 6));
- ICell cell132 = irow6.CreateCell(8);
- cell132.CellStyle = cellStylebt;
- ICell cell128 = irow6.CreateCell(9);
- cell128.CellStyle = cellStylebt;
- ICell cell129 = irow6.CreateCell(10);
- cell129.CellStyle = cellStylebt;
- ICell cell130 = irow6.CreateCell(11);
- cell130.CellStyle = cellStylebt;
- // sheet.AddMergedRegion(new CellRangeAddress(t + 1 + i, t + 1 + i, 8, 11));
- }
- }
- }
- }
-
- #endregion
- //自适应列宽度
- for (int i = 0; i < 12; i++)
- {
- // sheet.AutoSizeColumn(i);
- sheet.SetColumnWidth(i, 15 * 256);
- }
- using (MemoryStream ms = new MemoryStream())
- {
- workbook.Write(ms);
- HttpContext curContext = HttpContext.Current;
- // 设置编码和附件格式
- curContext.Response.ContentType = "application/vnd.ms-excel";
- curContext.Response.ContentEncoding = Encoding.UTF8;
- curContext.Response.Charset = "";
- curContext.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" + HttpUtility.UrlEncode("投诉产品日期分布表" + ".xls", Encoding.UTF8));
- curContext.Response.BinaryWrite(ms.GetBuffer());
- workbook = null;
- ms.Close();
- ms.Dispose();
- curContext.Response.End();
- }
- return "";
- }
- catch
- {
- return "导出失败!";
-
- }
- }
- }
- }
|