Files
GateDge2023_ljy/00.未分类/UnitTestProject2/UnitTest1.cs
PastSaid e1e6cba475 a
2024-04-22 09:39:19 +08:00

243 lines
7.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet;
using Spire.Doc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Kingdee.K3.SCM.App.Stock.Report;
namespace UnitTestProject2
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
//var a = new DynamicObject(new DynamicObjectType("COUNT_GAIN"));
//var a = new Dictionary<string, Dictionary<string, string>>();
//a.Add("a", new Dictionary<string, string>());
//a.Add("b", new Dictionary<string, string>());
//var keys = new[] { "a", "a", "b" };
//foreach (var key in keys)
//{
// var item = a[key];
// item.Add("ttt", "1232");
//}
var rowList = new List<string>();
var rowList2 = new List<List<string>>();
Random rd = new Random();
var r1 = rd.Next(200, 1000);
for (int i = 0; i < r1; i++)
{
rowList.Add($"100123,{i},100123,2024-04-07 00:00:00,100123,100123,T_STK_STKTRANSFERINENTRY1");
}
var len = rowList.Max(x => x.Length);
var num = 8000 / 100;
int ii = 0;
var flag = rowList.Any();
while (flag)
{
ii++;
var t = rowList.Skip(num * ii).Take(num).ToList();
flag = num * ii > 8000;
}
return;
}
[TestMethod]
public void TestMethod2()
{
string ip = "sftp.betagrid.gxs.com";
string port = "22";
string user = "HUIWE_SFTP_TST";
string pwd = "@m+n|9+v^.R}1[^";
string localPath = "D:\\Work\\GateDge\\SFTP\\aatat.docx";
string remotePath = "/aatat.docx";
SFTPHelper SFTPHelper = new SFTPHelper(ip, user, pwd, port);
var flag = SFTPHelper.Put(localPath, remotePath);
return;
}
public class SFTPHelper
{
#region
private SftpClient _sftp;
/// <summary>
/// SFTP连接状态
/// </summary>
public bool Connected => _sftp.IsConnected;
#endregion
#region
/// <summary>
/// 构造
/// </summary>
/// <param name="ip">IP</param>
/// <param name="port">端口</param>
/// <param name="user">用户名</param>
/// <param name="pwd">密码</param>
public SFTPHelper(string ip, string user, string pwd, string port = "22")
{
_sftp = new SftpClient(ip, int.Parse(port), user, pwd);
Connect();
}
~SFTPHelper()
{
Disconnect();
}
#endregion
#region SFTP
/// <summary>
/// 连接SFTP
/// </summary>
/// <returns>true成功</returns>
public bool Connect()
{
try
{
if (!Connected)
{
_sftp.Connect();
}
return true;
}
catch (Exception ex)
{
throw new Exception(string.Format("连接SFTP失败原因{0}", ex.Message));
}
}
#endregion
#region SFTP
/// <summary>
/// 断开SFTP
/// </summary>
public void Disconnect()
{
try
{
if (_sftp != null && Connected)
{
_sftp.Disconnect();
}
}
catch (Exception ex)
{
throw new Exception(string.Format("断开SFTP失败原因{0}", ex.Message));
}
}
#endregion
#region SFTP上传文件
/// <summary>
/// SFTP上传文件
/// </summary>
/// <param name="localPath">本地文件全路径 例G:\\Project\\logo.png</param>
/// <param name="remotePath">远程路径 例:/logo.png</param>
public bool Put(string localPath, string remotePath)
{
try
{
using (var file = File.OpenRead(localPath))
{
Connect();
_sftp.UploadFile(file, remotePath);
Disconnect();
}
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
#region SFTP获取文件
/// <summary>
/// SFTP获取文件
/// </summary>
/// <param name="remotePath">远程路径</param>
/// <param name="localPath">本地路径</param>
public void Get(string remotePath, string localPath)
{
try
{
Connect();
var byt = _sftp.ReadAllBytes(remotePath);
Disconnect();
File.WriteAllBytes(localPath, byt);
}
catch (Exception ex)
{
throw new Exception(string.Format("SFTP文件获取失败原因{0}", ex.Message));
}
}
#endregion
#region SFTP文件
/// <summary>
/// 删除SFTP文件
/// </summary>
/// <param name="remoteFile">远程路径</param>
public void Delete(string remoteFile)
{
try
{
Connect();
_sftp.Delete(remoteFile);
Disconnect();
}
catch (Exception ex)
{
throw new Exception(string.Format("SFTP文件删除失败原因{0}", ex.Message));
}
}
#endregion
}
[TestMethod]
public void ConvertHTMLtoWord()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Word|*.docx";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
string filepath = dialog.FileName;
//创建 Document 对象
Document document = new Document();
document.LoadFromFile(filepath);
string fileExs = System.IO.Path.GetExtension(filepath);
string fileName = System.IO.Path.GetFileNameWithoutExtension(filepath);
string newFilePath = "";
//newFilePath = filepath.Replace(fileExs, ".html");
//document.SaveToFile(newFilePath, FileFormat.Html);
//document.Close();
newFilePath = filepath.Replace(fileExs, ".xml");
//document.LoadFromFile(newFilePath, FileFormat.Html);
document.SaveToFile(newFilePath, FileFormat.Xml);
document.Close();
}
}
}
}