Commit 75f5cc42 authored by tr's avatar tr

1、增加NPOI导出Excel模板帮助类

parent 9e4d85a5
......@@ -111,7 +111,7 @@ namespace FunnyPick
/// <param name="drFrom">源行</param>
/// <param name="drTarget">目标行</param>
/// <param name="ExceptList">去除列(即其中包含的列不复制到目标行)</param>
public static void CopyRow(DataRow drFrom, DataRow drTarget, List<string> ExceptList = null)
public void CopyRow(DataRow drFrom, DataRow drTarget, List<string> ExceptList = null)
{
if (drFrom == null || drTarget == null)
return;
......
This diff is collapsed.
......@@ -4,4 +4,11 @@
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.24" />
<PackageReference Include="NPOI" Version="2.5.1" />
<PackageReference Include="RtfPipe" Version="1.0.7388.1242" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
</ItemGroup>
</Project>
using System.Data;
using System.Data.SqlClient;
namespace FunnyPick
{
internal class MySqlCommand : System.IDisposable
{
public void Dispose()
{
//TO DO: Support for using syntax
}
public DataTable Execute(string sql, CommandType type, params SqlParameter[] pars)
{
DataTable result = new DataTable();
SqlConnection conn = new SqlConnection(Utils.SqlServerLinkString);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = type;
cmd.Parameters.AddRange(pars);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.FillSchema(result, SchemaType.Mapped);
return result;
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
namespace FunnyPick
{
public static class Utils
{
public static string SqlServerLinkString => "Password=test;Persist Security Info=True;User ID=sa;Initial Catalog=TestDB;Data Source=.";
public static string StartupPath => AppContext.BaseDirectory;
public static string XmlPath => $@"{AppContext.BaseDirectory}\xml";
public static class UConvert
{
public static bool ToBool(object o)
{
bool result = false;
var b = bool.TryParse(o.ToString(), out result);
return result;
}
public static Decimal ToDecimal(object o, int decimals)
{
decimal result = 0;
var dec = decimal.TryParse(o.ToString(), out result);
return decimal.Round(result, decimals);
}
}
internal static void ShowMessage(string v)
{
throw new NotImplementedException();
}
internal static List<string> GetColumnList(DataTable dt, string groupCol)
{
return dt.Select().AsEnumerable().Select(x => x[groupCol].ToString()).Distinct().ToList();
}
/// <summary>
/// 复制两表(针对同结构,不同结构只复制相同列)
/// </summary>
/// <param name="dtFrom">源表</param>
/// <param name="dtTarget">目标表</param>
/// <param name="distinctCol">去重列</param>
/// <param name="AlwaysList">总是覆盖列(即包含在此列表中的列总是复制)</param>
/// <param name="ExceptList">去除列(即包含在此列表中的列不复制)</param>
internal static void CopyTable(DataTable dtFrom, DataTable dtTarget, string distinctCol = "", List<string> AlwaysList = null, List<string> ExceptList = null)
{
if (dtFrom == null || dtTarget == null)
return;
//
foreach (DataRow r in dtFrom.Rows)
{
if (IsIllegalDataRow(r))
continue;
//如果去重列不为空,并且两表都包含该列,则去除重复值
if (!string.IsNullOrEmpty(distinctCol) && dtFrom.Columns.Contains(distinctCol) && dtTarget.Columns.Contains(distinctCol))
{
DataRow[] rows = dtTarget.Select($"{distinctCol}='{r[distinctCol]}'");
if (rows.Length > 0 && AlwaysList != null)
{
foreach (var s in AlwaysList)
{
if (!dtFrom.Columns.Contains(s) || !dtTarget.Columns.Contains(s))
continue;
rows[0][s] = r[s];
}
rows[0].EndEdit();
continue;
}
}
//创建目标新行
DataRow drNew = dtTarget.NewRow();
CopyRow(r, drNew, ExceptList);
dtTarget.Rows.Add(drNew);
}
}
/// <summary>
/// 复制两行(针对相同结构,不同结构只复制相同列)
/// </summary>
/// <param name="drFrom">源行</param>
/// <param name="drTarget">目标行</param>
/// <param name="ExceptList">去除列(即其中包含的列不复制到目标行)</param>
internal static void CopyRow(DataRow drFrom, DataRow drTarget, List<string> ExceptList = null)
{
if (drFrom == null || drTarget == null)
return;
//
foreach (DataColumn c in drFrom.Table.Columns)
{
string ColName = c.ColumnName;
//ID和指定列不复制
if (ColName.Equals("ID") || ExceptList?.IndexOf(ColName) >= 0)
continue;
if (drTarget.Table.Columns.Contains(ColName))
{
drTarget[ColName] = drFrom[ColName];
}
drTarget.EndEdit();
}
}
/// <summary>
/// 是否无效行
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
internal static bool IsIllegalDataRow(DataRow dr)
{
return dr == null
|| dr.RowState == DataRowState.Deleted
|| dr.RowState == DataRowState.Detached;
}
internal static string DownloadRtf(string v)
{
throw new NotImplementedException();
}
internal static byte[] FetchImageSingle(byte[] bytes)
{
throw new NotImplementedException();
}
}
}
D:\source\Ftt_Common\FunnyPick\FunnyPick\bin\Debug\netcoreapp3.0\FunnyPick.deps.json
D:\source\Ftt_Common\FunnyPick\FunnyPick\bin\Debug\netcoreapp3.0\FunnyPick.dll
D:\source\Ftt_Common\FunnyPick\FunnyPick\obj\Debug\netcoreapp3.0\FunnyPick.csprojAssemblyReference.cache
D:\source\Ftt_Common\FunnyPick\FunnyPick\obj\Debug\netcoreapp3.0\FunnyPick.AssemblyInfoInputs.cache
D:\source\Ftt_Common\FunnyPick\FunnyPick\obj\Debug\netcoreapp3.0\FunnyPick.AssemblyInfo.cs
D:\source\Ftt_Common\FunnyPick\FunnyPick\bin\Debug\netcoreapp3.0\FunnyPick.pdb
D:\source\Ftt_Common\FunnyPick\FunnyPick\obj\Debug\netcoreapp3.0\FunnyPick.dll
D:\source\Ftt_Common\FunnyPick\FunnyPick\obj\Debug\netcoreapp3.0\FunnyPick.pdb
{
"version": 1,
"dgSpecHash": "fw80HZqvm5AVgRwJbMlF6y3GYKRDQ1jrGvQ1sh3sZbiI99GtL6KoEjTsP3CBjWXIjs3fgrgf8hRx5j+6D1vZjA==",
"dgSpecHash": "U/yfLlYhboZkdycMmaL/82PW85/BYUR9MtNXES706SvOVrkKFO3vUNrQkMMe0XE1mYt+wbTPHZrL1Nd1ZRdjUA==",
"success": true
}
\ No newline at end of file
......@@ -40,6 +40,24 @@
},
"frameworks": {
"netcoreapp3.0": {
"dependencies": {
"HtmlAgilityPack": {
"target": "Package",
"version": "[1.11.24, )"
},
"NPOI": {
"target": "Package",
"version": "[2.5.1, )"
},
"RtfPipe": {
"target": "Package",
"version": "[1.0.7388.1242, )"
},
"System.Data.SqlClient": {
"target": "Package",
"version": "[4.8.1, )"
}
},
"imports": [
"net461",
"net462",
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment