我想在C#中将多个Excel文件与EPPlus合并.
我做了以下事情:
using (MemoryStream protocolStream = new MemoryStream())
{
ExcelPackage pck = new ExcelPackage();
HashSet<string> wsNames = new HashSet<string>();
foreach (var file in files)
{
ExcelPackage copyPck = new ExcelPackage(new FileInfo(file));
foreach (var ws in copyPck.Workbook.Worksheets)
{
string name = ws.Name;
int i = 1;
while (!wsNames.Add(ws.Name))
name = ws.Name + i++;
ws.Name = name;
var copiedws = pck.Workbook.Worksheets.Add(name);
copiedws.WorksheetXml.LoadXml(ws.WorksheetXml.DocumentElement.OuterXml);
}
}
pck.SaveAs(protocolStream);
protocolStream.Position = 0;
using (FileStream fs = new FileStream(resultFile, FileMode.Create))
protocolStream.CopyTo(fs);
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误pck.SaveAs(protocolStream):
System.ArgumentOutOfRangeException
System.ChrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument参数,ExceptionResource资源)在System.Collections.Generic.List …
我正在创建一个 xsl stylehseet 并提出了这个(在我看来是不合逻辑的行为):
这个 XPath:
/root/element[1][@attr1 != '1' 或 @attr2 != 'test']
比这个 XPath 慢得多:
/root/element[count(preceding-sibling::element) + 1 = 1) and (@attr1 != '1' or @attr2 != 'test')]
我有 50 个示例 xml,第一个 XPath 需要大约 55 秒。
使用第二个 XPath 需要大约 4 秒!
我使用 XslCompiledTransform (C# .NET 4.5)。
有人可以解释为什么第一个 XPath 比第二个慢得多吗?我一直认为最好使用显式索引过滤器。
更新:一些示例 xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<element attr2="test" attr1="1">
<child>17</child>
<child>17</child>
<child>16</child>
...
<child>3</child>
<child>2</child>
<child>1</child>
</element>
<element attr2="test2" attr1="2">
<child/>
<child/>
<child/>
<child/>
<child/>
<child/>
<child/>
...
<child/>
</element> …Run Code Online (Sandbox Code Playgroud) 我有一个Windows服务,它在运行时在另一个AppDomain中加载程序集.然后它执行它们,最后卸载AppDomain.问题是来自插件的execute方法是异步任务,我得到SerializationException,因为Task不从MarshalByRefObject继承.
我将插件包装在继承自MarshalByRefObject的代理中,但我不知道如何摆脱SerializationException?
public interface IPlugin : IDisposable
{
Guid GUID { get; }
string Name { get; }
string Description { get; }
Task Execute(PluginPanel panel, string user);
}
Run Code Online (Sandbox Code Playgroud)
代理人:
[Serializable()]
public class PluginProxy : MarshalByRefObject, IPlugin
{
private IPlugin m_Plugin;
public bool Init(string file)
{
Assembly ass = Assembly.Load(AssemblyName.GetAssemblyName(file));
if (ass == null || ass.GetTypes() == null || ass.GetTypes().Length == 0)
return false;
foreach (Type type in ass.GetTypes())
{
if (type.IsInterface || type.IsAbstract)
continue;
if (type.GetInterface(typeof(IPlugin).FullName) != null)
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用应用程序文档文件夹中的本地HTML从UIWebView迁移到WKWebView。我可以将所有css和js文件加载到索引页面,但是由于允许访问源,每个ajax调用(xmlhttprequest)都会失败。
我不想在我的应用程序中使用Web服务器,因为我认为它会过大。我怎样才能实现呢?该应用程序是内部应用程序的简单HTML5应用程序。设备无法上网或其他任何原因,因此可能会完全禁用安全性。
我阅读了 Task.Run 和 Task.Factory.StartNew 的差异。
Task.Run(() => {});
Run Code Online (Sandbox Code Playgroud)
应该相当于
Task.Factory.StartNew(() => {}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
Run Code Online (Sandbox Code Playgroud)
但是在我的代码中,我希望由于 Task.Factory.StartNew 而不会发生死锁:
private Task backgroundTask;
private async Task DoSomethingAsync()
{
// this should deadlock
await this.backgroundTask.ConfigureAwait(false);
throw new Exception();
}
private async Task Test()
{
this.backgroundTask = Task.Factory.StartNew(async () =>
{
await this.DoSomethingAsync().ConfigureAwait(false);
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
// just wait here for testing/debugging
await Task.Delay(10000).ConfigureAwait(false);
// if no deadlock, this should throw
await this.backgroundTask.ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)
但这并不是死锁。DoSomethingAsync 中的异常被抛出但从未被捕获。在 Task.Delay 之后等待 Task 也不会抛出,因为它是 RanToCompletion。
使用 Task.Run …
我有一个应用程序,通常有1.000 - 30.000文件转换一些数据.
我需要做3个步骤:
所以这三个步骤包括一些I/O,我使用了async/await方法:
var tasks = files.Select(async (file) =>
{
Item item = await createtempFile(file).ConfigureAwait(false);
await convert(item).ConfigureAwait(false);
await clean(item).ConfigureAwait(false);
}).ToList();
await Task.WhenAll(tasks).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)
我不知道这是否是最好的做法,因为我创造了超过一千个任务.我想过将这三个步骤拆分为:
List<Item> items = new List<Item>();
var tasks = files.Select(async (file) =>
{
Item item = await createtempFile(file, ext).ConfigureAwait(false);
lock(items)
items.Add(item);
}).ToList();
await Task.WhenAll(tasks).ConfigureAwait(false);
var tasks = items.Select(async (item) =>
{
await convert(item, baseAddress, ext).ConfigureAwait(false);
}).ToList();
await Task.WhenAll(tasks).ConfigureAwait(false);
var tasks = items.Select(async (item) =>
{
await clean(targetFile, item.Doctype, ext).ConfigureAwait(false);
}).ToList();
await Task.WhenAll(tasks).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)
但这似乎没有更好或更快,因为我创造了数千次任务. …
c# ×5
async-await ×2
asynchronous ×2
task ×2
ajax ×1
appdomain ×1
deadlock ×1
epplus ×1
excel ×1
ios ×1
javascript ×1
performance ×1
wkwebview ×1
xml ×1
xpath ×1
xslt ×1