1 回答

TA貢獻1798條經(jīng)驗 獲得超7個贊
好的....這就是我最終的結果。它可能不漂亮,但它確實有效。
首先,我更改了單擊事件以將更新與模板文件的傳輸分開。
private async void FileButton_Click(object sender, RoutedEventArgs e)
{
await FileHelper.GetFileAsync();
await FileHelper.UpdateCell(FileHelper.saveLocation, App.Date, 2, "D");
await FileHelper.UpdateCell(FileHelper.saveLocation, App.Maximo, 3, "D");
...
await FileHelper.UpdateCell(FileHelper.saveLocation, App.StopHours, 26, "J");
}
然后我將 GetFileAsync() 和 UpdateCell() 都更新為 Task 而不是 void。然后,當我得到 await Task.Run 時,我添加了一個返回 TaskStatus.RanToCompletion。
class FileHelper
{
public static string saveLocation;
public static SpreadsheetDocument spreadsheetDoc;
public static async Task GetFileAsync()
{
var (authResult, message) = await Authentication.AquireTokenAsync();
var httpClient = new HttpClient();
HttpResponseMessage response;
var request = new HttpRequestMessage(HttpMethod.Get, MainPage.fileurl);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
response = await httpClient.SendAsync(request);
byte[] fileBytes = await response.Content.ReadAsByteArrayAsync();
StorageLibrary videoLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos);
string saveFolder = videoLibrary.SaveFolder.Path;
string saveFileName = App.Date + "-" + App.StartTime + "-" + App.IBX + "-" + App.Generator + ".xlsx";
saveLocation = saveFolder + "\\" + saveFileName;
using (MemoryStream stream = new MemoryStream())
{
stream.Write(fileBytes, 0, (int)fileBytes.Length);
using (spreadsheetDoc = SpreadsheetDocument.Open(stream, true))
{
await Task.Run(() =>
{
File.WriteAllBytes(saveLocation, stream.ToArray());
return TaskStatus.RanToCompletion;
});
}
}
}
public async static Task UpdateCell(string docName, string text,
uint rowIndex, string columnName)
{
StorageLibrary videoLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos);
string saveFolder = videoLibrary.SaveFolder.Path;
string saveFileName = App.Date + "-" + App.StartTime + "-" + App.IBX + "-" + App.Generator + ".xlsx";
saveLocation = saveFolder + "\\" + saveFileName;
await Task.Run(() =>
{
using (spreadsheetDoc = SpreadsheetDocument.Open(saveLocation, true))
{
WorksheetPart worksheetPart =
GetWorksheetPartByName(spreadsheetDoc, "GenRun");
if (worksheetPart != null)
{
Cell cell = GetCell(worksheetPart.Worksheet,
columnName, rowIndex);
cell.CellValue = new CellValue(text);
cell.DataType =
new EnumValue<CellValues>(CellValues.String);
worksheetPart.Worksheet.Save();
}
}
return TaskStatus.RanToCompletion;
});
}
private static WorksheetPart
GetWorksheetPartByName(SpreadsheetDocument document,
string sheetName)
{
IEnumerable<Sheet> sheets =
document.WorkbookPart.Workbook.GetFirstChild<Sheets>().
Elements<Sheet>().Where(s => s.Name == sheetName);
if (sheets.Count() == 0)
{
return null;
}
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)
document.WorkbookPart.GetPartById(relationshipId);
return worksheetPart;
}
private static Cell GetCell(Worksheet worksheet,
string columnName, uint rowIndex)
{
Row row = GetRow(worksheet, rowIndex);
if (row == null)
return null;
return row.Elements<Cell>().Where(c => string.Compare
(c.CellReference.Value, columnName +
rowIndex, true) == 0).First();
}
private static Row GetRow(Worksheet worksheet, uint rowIndex)
{
return worksheet.GetFirstChild<SheetData>().
Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
}
}
- 1 回答
- 0 關注
- 156 瀏覽
添加回答
舉報