当前位置:   article > 正文

npoi绘制文件存储服务器并把文件转文件流存储数据库_npoi转成流

npoi转成流
  1. 1.数据库 设计类型
  2. FileContent nvarchar(100) 存储文件流
  1. npoi 绘制 文档 workbook
  2. HSSFWorkbook workbook = new HSSFWorkbook();
  3. 具体操作省略....
  4. 绘制好后存储至服务器某个文件夹下
  5. string source = System.Windows.Forms.Application.StartupPath + "\\Temp";//文件路径
  6. string FileName = "月度生产经营数据" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";
  7. string FilePath = System.Windows.Forms.Application.StartupPath + "\\Temp\\" + FileName;//文件路径
  8. //如果路径不存在,创建路径
  9. if (!Directory.Exists(source))
  10. {
  11. Directory.CreateDirectory(source); //创建一个文件路径 存储文档信息
  12. }
  13. //转为字节数组
  14. MemoryStream stream = new MemoryStream();
  15. workbook.Write(stream);
  16. var buf = stream.ToArray();
  17. //保存为Excel文件
  18. using (FileStream fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write))
  19. {
  20. fs.Write(buf, 0, buf.Length);
  21. fs.Flush();
  22. }
  1. #region excel 文件信息入库
  2. FileInfor model =new FileInfor();
  3. model.FileName = "文件名称";
  4. model.FilePath = FileName;
  5. model.FileSize = new System.IO.FileInfo(FilePath).Length.ToString();
  6. model.FileType = ".xls";
  7. model.CreateDate = now.ToString();
  8. model.StartDate = week1.ToString("yyyy-MM-dd");
  9. model.EndedDate = week5.ToString("yyyy-MM-dd");
  10. byte[] stream_file = FileToStream(FilePath);
  11. model.FileContent = stream_file; //文件转流
  12. string sql = data.sqlMonthProducOperate(model);
  13. textBox1.AppendText(sql);
  14. #endregion
  15. SQL server 入库 简易版
  16. public string sqlMonthProducOperate(FileInfor data)
  17. {
  18. try
  19. {
  20. //接口 链接方式
  21. //string aa = WebConfigurationManager.AppSettings["ConnectionString"];
  22. //winfrom 链接方式
  23. string aa = System.Configuration.ConfigurationSettings.AppSettings["connectionstr"];
  24. using (SqlConnection con = new SqlConnection(aa))
  25. {
  26. con.Open();
  27. using (SqlCommand cmd = new SqlCommand(@" insert into [dbo].[Sys_MonthProducOperate]
  28. ([ID]
  29. ,[FileName]
  30. ,[FilePath]
  31. ,[FileType]
  32. ,[FileSize]
  33. ,[CreateDate]
  34. ,[StartDate]
  35. ,[EndedDate],FileContent) values
  36. (NEWID(),'" + data.FileName + @"','" + data.FilePath + @"','" + data.FileType + @"','" + data.FileSize + @"'
  37. ,'" + data.CreateDate + @"','" + data.StartDate + @"','" + data.EndedDate + @"',@FileContent)", con))
  38. {
  39. cmd.Parameters.Add("@FileContent", SqlDbType.VarBinary).Value = data.FileContent; //此处注意写入类型
  40. cmd.ExecuteNonQuery();
  41. }
  42. }
  43. return "-- 日志 -- 成功-sql入库成功 ---" + DateTime.Now + "\r\n";
  44. }
  45. catch (Exception ex)
  46. {
  47. return "-- 日志 -- 失败-sql入库失败原因:"+ex.Message+" ---" + DateTime.Now + "\r\n";
  48. }
  49. }
  50. }
  51. public class FileInfor
  52. {
  53. public string FileName { get; set; }
  54. public string FilePath { get; set; }
  55. public string FileType { get; set; }
  56. public string FileSize { get; set; }
  57. public string CreateDate { get; set; }
  58. public string StartDate { get; set; }
  59. public string EndedDate { get; set; }
  60. public byte[] FileContent { get; set; }
  61. }
  62. #region
  63. /// <summary>
  64. /// 文件转流
  65. /// </summary>
  66. /// <param name="fileName">文件路径</param>
  67. /// <param name="isDelete">是否删除临时文件</param>
  68. /// <returns></returns>
  69. public static byte[] FileToStream(string fileName, bool isDelete = false)
  70. {
  71. //打开文件
  72. FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  73. // 读取文件的 byte[]
  74. byte[] bytes = new byte[fileStream.Length];
  75. fileStream.Read(bytes, 0, bytes.Length);
  76. fileStream.Close();
  77. // 把 byte[] 转换成 Stream
  78. //Stream stream = new MemoryStream(bytes);
  79. if (isDelete)
  80. {
  81. File.Delete(fileName);//删除临时文件
  82. }
  83. return bytes;
  84. }
  85. #endregion

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/216155
推荐阅读
相关标签
  

闽ICP备14008679号