Преглед изворни кода

增加Excel操作类实现简单操作

xuwei пре 2 година
родитељ
комит
4b4fea8ebb

+ 46 - 0
wwwroot/App_Code/ExcelFile.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Web;
+using NPOI.SS.UserModel;
+
+/// <summary>
+/// 操作ExcelFile的简单方法
+/// xuwei add 2024-01-05 
+/// </summary>
+public class ExcelFile
+{
+    public IWorkbook GetWorkbook;
+
+    public ExcelFile()
+    {
+
+    }
+
+    public ExcelFile(string fileName)
+    {
+        Open(fileName);
+    }
+
+    //打开excel
+    public void Open(string fileName)
+    {
+        FileStream fs = File.Open(HttpContext.Current.Server.MapPath(fileName), FileMode.Open, FileAccess.Read);
+        GetWorkbook = WorkbookFactory.Create(fs);
+        fs.Close();
+    }
+
+    //保存excel
+    public void SaveAs(string fileName)
+    {
+        MemoryStream ms = new MemoryStream();
+        GetWorkbook.Write(ms);
+        FileStream nfs = new FileStream(HttpContext.Current.Server.MapPath(fileName), FileMode.Create, FileAccess.Write);
+        nfs.Write(ms.ToArray(), 0, ms.ToArray().Length);
+        nfs.Flush();
+        nfs.Close();
+        ms.Close();
+    }
+
+}

BIN
wwwroot/Bin/NPOI.OOXML.dll


BIN
wwwroot/Bin/NPOI.OOXML.dll.refresh


BIN
wwwroot/Bin/NPOI.OpenXml4Net.dll


+ 1 - 0
wwwroot/Bin/NPOI.OpenXml4Net.dll.refresh

@@ -0,0 +1 @@
+..\packages\NPOI.Excel.2.1.1\lib\NPOI.OpenXml4Net.dll

BIN
wwwroot/Bin/NPOI.OpenXmlFormats.dll


+ 1 - 0
wwwroot/Bin/NPOI.OpenXmlFormats.dll.refresh

@@ -0,0 +1 @@
+..\packages\NPOI.Excel.2.1.1\lib\NPOI.OpenXmlFormats.dll

BIN
wwwroot/Bin/NPOI.dll


BIN
wwwroot/Bin/NPOI.dll.refresh


+ 3 - 3
wwwroot/Web.config

@@ -6,14 +6,14 @@
 <configuration>
 	<connectionStrings>
 		<!--三水正式服务器-->
-		<!--<add name="ConnectionString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.18.32.116)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=dkmes)));User Id=hgiboss;Password=dongke" providerName="Oracle.ManagedDataAccess.Client"/>-->
+		<add name="ConnectionString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.18.32.31)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=dkmes)));User Id=dkmes;Password=dongke" providerName="Oracle.ManagedDataAccess.Client"/>
 		<!--三水测试服务器-->
-		<add name="ConnectionString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.18.32.116)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=dkmes)));User Id=hgiboss;Password=dongke" providerName="Oracle.ManagedDataAccess.Client"/>
+		<!--<add name="ConnectionString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.18.32.116)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=dkmes)));User Id=hgiboss;Password=dongke" providerName="Oracle.ManagedDataAccess.Client"/>-->
 	</connectionStrings>
 	<appSettings>
 		
 		<!--成检 服务地址-->
-		<add key="ProductCheckServer" value="http://172.18.32.32:5678"/>
+		<add key="ProductCheckServer" value="http://172.18.32.31:5678"/>
 		<!--<add key="ProductCheckServer" value="http://172.18.32.31:5678"/>-->
 		<!--<add key="ProductCheckServer" value="http://172.18.32.30:1234"/>-->
 		<!--<add key="ProductCheckServer" value="http://127.0.0.1:1234"/>-->

+ 34 - 0
wwwroot/main/excel/excelRW.ashx

@@ -0,0 +1,34 @@
+<%@ WebHandler Language="C#" Class="excelRW" %>
+
+using System;
+using System.Web;
+using NPOI.SS.UserModel;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+public class excelRW : IHttpHandler
+{
+
+    public void ProcessRequest(HttpContext context)
+    {
+        context.Response.ContentType = "text/html";
+
+        ExcelFile excel = new ExcelFile("template.xls");
+        excel.GetWorkbook.GetSheet("封面").GetRow(14).GetCell(6).SetCellValue("东科商标");
+        excel.GetWorkbook.GetSheet("内容").GetRow(1).GetCell(15).SetCellValue("东科检验项目");
+        excel.GetWorkbook.GetSheet("内容").GetRow(1).GetCell(23).SetCellValue("东科检验结果");
+        excel.SaveAs("export.xls");
+        context.Response.Write("文件写入成功!<a href='export.xls'>点击下载</a>");
+    }
+
+    public bool IsReusable
+    {
+        get
+        {
+            return false;
+        }
+    }
+
+}

BIN
wwwroot/main/excel/template.xls


+ 48 - 0
wwwroot/website.publishproj

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+
+***********************************************************************************************
+website.publishproj
+
+警告: 请勿修改此文件,它将用于 Web 发布过程。
+
+版权所有 (C) Microsoft Corporation。保留所有权利。
+
+***********************************************************************************************
+-->
+
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>10.0.30319</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{47cb0c52-45ca-43ea-b3aa-f945ca93e7c2}</ProjectGuid>
+    <SourceWebPhysicalPath>$(MSBuildThisFileDirectory)</SourceWebPhysicalPath>
+    <SourceWebVirtualPath>/wwwroot</SourceWebVirtualPath>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <SourceWebProject></SourceWebProject>
+    <SourceWebMetabasePath></SourceWebMetabasePath>
+  </PropertyGroup>
+  <PropertyGroup>
+    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
+    <!-- for VS2010 we need to use 10.5 but for VS2012+ we should use VisualStudioVersion -->
+    <WebPublishTargetsVersion Condition=" '$(WebPublishTargetsVersion)' =='' and '$(VisualStudioVersion)' == 10.0 ">10.5</WebPublishTargetsVersion>
+    <WebPublishTargetsVersion Condition=" '$(WebPublishTargetsVersion)'=='' ">$(VisualStudioVersion)</WebPublishTargetsVersion>
+
+    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(WebPublishTargetsVersion)</VSToolsPath>
+    <_WebPublishTargetsPath Condition=" '$(_WebPublishTargetsPath)'=='' ">$(VSToolsPath)</_WebPublishTargetsPath>
+    <AssemblyFileVersion Condition="'$(AssemblyFileVersion)' == ''">1.0.0.0</AssemblyFileVersion>
+    <AssemblyVersion Condition="'$(AssemblyVersion)' == ''">1.0.0.0</AssemblyVersion>
+  </PropertyGroup>
+  <ItemGroup>
+    <AssemblyAttributes Include="AssemblyFileVersion">
+      <Value>$(AssemblyFileVersion)</Value>
+    </AssemblyAttributes>
+    <AssemblyAttributes Include="AssemblyVersion">
+      <Value>$(AssemblyVersion)</Value>
+    </AssemblyAttributes>
+  </ItemGroup>
+  <Import Project="$(_WebPublishTargetsPath)\Web\Microsoft.WebSite.Publishing.targets" />
+</Project>