|
|
@@ -8,9 +8,12 @@ using NPOI.HSSF.UserModel;
|
|
8
|
8
|
using NPOI.SS.UserModel;
|
|
9
|
9
|
using NPOI.SS.Util;
|
|
10
|
10
|
using NPOI.XSSF.UserModel;
|
|
11
|
|
-using CallCenterApi.DB;
|
|
12
|
11
|
using System.Collections.Generic;
|
|
13
|
12
|
using System.Linq;
|
|
|
13
|
+using CallCenterApi.DB;
|
|
|
14
|
+using System.Threading.Tasks;
|
|
|
15
|
+using System.Net.Http;
|
|
|
16
|
+using System.Net;
|
|
14
|
17
|
|
|
15
|
18
|
namespace CallCenter.Utility
|
|
16
|
19
|
{
|
|
|
@@ -1806,15 +1809,9 @@ namespace CallCenter.Utility
|
|
1806
|
1809
|
cell14.SetCellValue(complaints[i].name);
|
|
1807
|
1810
|
cell14.CellStyle = cellStylebt;
|
|
1808
|
1811
|
break;
|
|
1809
|
|
-
|
|
1810
|
1812
|
}
|
|
1811
|
|
-
|
|
1812
|
1813
|
}
|
|
1813
|
|
-
|
|
1814
|
|
-
|
|
1815
|
|
-
|
|
1816
|
1814
|
}
|
|
1817
|
|
-
|
|
1818
|
1815
|
//自适应列宽度
|
|
1819
|
1816
|
for (int i = 0; i < 10; i++)
|
|
1820
|
1817
|
{
|
|
|
@@ -1853,5 +1850,184 @@ namespace CallCenter.Utility
|
|
1853
|
1850
|
}
|
|
1854
|
1851
|
|
|
1855
|
1852
|
|
|
1856
|
|
- }
|
|
|
1853
|
+ /// <summary>
|
|
|
1854
|
+ /// NPOI导出EXCEL
|
|
|
1855
|
+ /// </summary>
|
|
|
1856
|
+ /// <param name="dt">数据源</param>
|
|
|
1857
|
+ /// <param name="title">导出文件的名称</param>
|
|
|
1858
|
+ /// <param name="array">列宽数组</param>
|
|
|
1859
|
+ public string NpoiExcel(DataTable table,string F_ParamValue)
|
|
|
1860
|
+ {
|
|
|
1861
|
+ try {
|
|
|
1862
|
+
|
|
|
1863
|
+ NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
|
|
|
1864
|
+ NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
|
|
|
1865
|
+
|
|
|
1866
|
+ NPOI.SS.UserModel.IRow headerrow = sheet.CreateRow(0);
|
|
|
1867
|
+ headerrow.Height = 30 * 20;
|
|
|
1868
|
+ ICellStyle style = book.CreateCellStyle();
|
|
|
1869
|
+ style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
1870
|
+ style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
1871
|
+ style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
1872
|
+ style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
|
|
1873
|
+ style.WrapText = true;
|
|
|
1874
|
+ style.Alignment = HorizontalAlignment.Center;
|
|
|
1875
|
+ style.VerticalAlignment = VerticalAlignment.Center;
|
|
|
1876
|
+
|
|
|
1877
|
+ List<Workorderpicture> workorderpictures = new List<Workorderpicture>();
|
|
|
1878
|
+ foreach (DataRow dr in table.Rows)
|
|
|
1879
|
+ {
|
|
|
1880
|
+ Workorderpicture workorderpicture = new Workorderpicture();
|
|
|
1881
|
+ workorderpicture.picture = new List<string>();
|
|
|
1882
|
+ var FileUrl = GetFileData(dr["F_DealFile"].ToString (), F_ParamValue);
|
|
|
1883
|
+ workorderpicture.workorder = dr["F_WorkOrderId"].ToString();
|
|
|
1884
|
+ foreach (var dt in FileUrl )
|
|
|
1885
|
+ {
|
|
|
1886
|
+ workorderpicture.picture.Add(dt);
|
|
|
1887
|
+ }
|
|
|
1888
|
+ workorderpictures.Add(workorderpicture);
|
|
|
1889
|
+ }
|
|
|
1890
|
+ var count = workorderpictures.Select(x => x.picture.Count).Max();
|
|
|
1891
|
+ //建立内容行
|
|
|
1892
|
+ int iRowIndex = 0;
|
|
|
1893
|
+ foreach (var it in workorderpictures)
|
|
|
1894
|
+ {
|
|
|
1895
|
+ int iCellIndex = 0;
|
|
|
1896
|
+ IRow irow = sheet.CreateRow(iRowIndex );
|
|
|
1897
|
+ for (int i = 0; i < count; i++)
|
|
|
1898
|
+ {
|
|
|
1899
|
+ string strsj = string.Empty;
|
|
|
1900
|
+ if (iCellIndex == 0)
|
|
|
1901
|
+ {
|
|
|
1902
|
+ ICell cell = irow.CreateCell(iCellIndex);
|
|
|
1903
|
+ cell.SetCellValue(it.workorder);
|
|
|
1904
|
+ cell.CellStyle = style;
|
|
|
1905
|
+ iCellIndex++;
|
|
|
1906
|
+ }
|
|
|
1907
|
+ else
|
|
|
1908
|
+ {
|
|
|
1909
|
+ if (it .picture .Count <= count)
|
|
|
1910
|
+ {
|
|
|
1911
|
+
|
|
|
1912
|
+ ICell cell = irow.CreateCell(iCellIndex);
|
|
|
1913
|
+ // cell.SetCellValue(it.workorder);
|
|
|
1914
|
+ if (iCellIndex<= it.picture.Count)
|
|
|
1915
|
+ {
|
|
|
1916
|
+ bool n= AddPieChartAsync(book, sheet, it.picture[iCellIndex - 1], iRowIndex, iCellIndex, 1000);
|
|
|
1917
|
+ if (!n )
|
|
|
1918
|
+ cell.SetCellValue("图片不存在!");
|
|
|
1919
|
+ }
|
|
|
1920
|
+ else
|
|
|
1921
|
+ cell.SetCellValue("");
|
|
|
1922
|
+ cell.CellStyle = style;
|
|
|
1923
|
+ iCellIndex++;
|
|
|
1924
|
+
|
|
|
1925
|
+ }
|
|
|
1926
|
+ }
|
|
|
1927
|
+
|
|
|
1928
|
+ }
|
|
|
1929
|
+ iRowIndex++;
|
|
|
1930
|
+
|
|
|
1931
|
+ }
|
|
|
1932
|
+
|
|
|
1933
|
+
|
|
|
1934
|
+ //自适应列宽度
|
|
|
1935
|
+ for (int i = 0; i < count; i++)
|
|
|
1936
|
+ {
|
|
|
1937
|
+ sheet.AutoSizeColumn(800);
|
|
|
1938
|
+ }
|
|
|
1939
|
+
|
|
|
1940
|
+ using (MemoryStream ms = new MemoryStream())
|
|
|
1941
|
+ {
|
|
|
1942
|
+ book.Write(ms);
|
|
|
1943
|
+
|
|
|
1944
|
+ HttpContext curContext = HttpContext.Current;
|
|
|
1945
|
+
|
|
|
1946
|
+
|
|
|
1947
|
+ // 设置编码和附件格式
|
|
|
1948
|
+ curContext.Response.ContentType = "application/vnd.ms-excel";
|
|
|
1949
|
+ curContext.Response.ContentEncoding = Encoding.UTF8;
|
|
|
1950
|
+ curContext.Response.Charset = "";
|
|
|
1951
|
+ curContext.Response.AppendHeader("Content-Disposition",
|
|
|
1952
|
+ "attachment;filename=" + HttpUtility.UrlEncode("工单列表" + ".xls", Encoding.UTF8));
|
|
|
1953
|
+
|
|
|
1954
|
+ curContext.Response.BinaryWrite(ms.GetBuffer());
|
|
|
1955
|
+
|
|
|
1956
|
+ book = null;
|
|
|
1957
|
+ ms.Close();
|
|
|
1958
|
+ ms.Dispose();
|
|
|
1959
|
+
|
|
|
1960
|
+ curContext.Response.End();
|
|
|
1961
|
+ }
|
|
|
1962
|
+
|
|
|
1963
|
+ return "";
|
|
|
1964
|
+ }
|
|
|
1965
|
+ catch (Exception e)
|
|
|
1966
|
+ {
|
|
|
1967
|
+ return "导出失败!" + e.Message;
|
|
|
1968
|
+ }
|
|
|
1969
|
+ }
|
|
|
1970
|
+ public class Workorderpicture
|
|
|
1971
|
+ {
|
|
|
1972
|
+ public string workorder;
|
|
|
1973
|
+ public List<string> picture;
|
|
|
1974
|
+ }
|
|
|
1975
|
+ public List<string> GetFileData(string ids, string prefix)
|
|
|
1976
|
+ {
|
|
|
1977
|
+ List<string> F_Url = new List<string>();
|
|
|
1978
|
+ DataTable dt = new DataTable();
|
|
|
1979
|
+ if (!string.IsNullOrEmpty(ids))
|
|
|
1980
|
+ {
|
|
|
1981
|
+ dt = DbHelperSQL.Query("select * from T_Sys_Accessories where F_Id in (" + ids + ")").Tables[0];
|
|
|
1982
|
+ foreach (DataRow dr in dt.Rows)
|
|
|
1983
|
+ {
|
|
|
1984
|
+ string Url = prefix + dr["F_Url"].ToString();
|
|
|
1985
|
+ F_Url.Add(Url);
|
|
|
1986
|
+ }
|
|
|
1987
|
+ }
|
|
|
1988
|
+ return F_Url;
|
|
|
1989
|
+ }
|
|
|
1990
|
+ /// <summary>
|
|
|
1991
|
+ /// 向sheet插入图片
|
|
|
1992
|
+ /// </summary>
|
|
|
1993
|
+ /// <param name="workbook">工作辅</param>
|
|
|
1994
|
+ /// <param name="sheet">sheet页</param>
|
|
|
1995
|
+ /// <param name="fileurl">图片地址</param>
|
|
|
1996
|
+ /// <param name="row">当前行</param>
|
|
|
1997
|
+ /// <param name="col">当前列</param>
|
|
|
1998
|
+ /// <param name="RowHeight">行高</param>
|
|
|
1999
|
+ public bool AddPieChartAsync(HSSFWorkbook workbook, ISheet sheet, string fileurl, int row, int col, short RowHeight)
|
|
|
2000
|
+ {
|
|
|
2001
|
+ WebClient myWebClient = new WebClient();
|
|
|
2002
|
+ myWebClient.Credentials = CredentialCache.DefaultCredentials;
|
|
|
2003
|
+ myWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
|
|
|
2004
|
+ try
|
|
|
2005
|
+ {
|
|
|
2006
|
+ byte[] bytes = myWebClient.DownloadData(fileurl);
|
|
|
2007
|
+ if (bytes != null)
|
|
|
2008
|
+ {
|
|
|
2009
|
+ int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
|
|
|
2010
|
+ HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
|
|
|
2011
|
+ HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 20, 20, col, row, col + 1, row + 1);
|
|
|
2012
|
+ //##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50
|
|
|
2013
|
+
|
|
|
2014
|
+ HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
|
|
|
2015
|
+ var index = sheet.GetRow(row) ?? sheet.CreateRow(row);
|
|
|
2016
|
+ index.Height = RowHeight;
|
|
|
2017
|
+ //pict.Resize(0);//这句话一定不要,这是用图片原始大小来显示
|
|
|
2018
|
+
|
|
|
2019
|
+ }
|
|
|
2020
|
+ return true;
|
|
|
2021
|
+ }
|
|
|
2022
|
+ catch
|
|
|
2023
|
+ {
|
|
|
2024
|
+ return false ;
|
|
|
2025
|
+ }
|
|
|
2026
|
+
|
|
|
2027
|
+
|
|
|
2028
|
+
|
|
|
2029
|
+ }
|
|
|
2030
|
+
|
|
|
2031
|
+
|
|
|
2032
|
+}
|
|
1857
|
2033
|
}
|