我有一个UWP应用。我已经从SharePoint驱动器中取出了一个excel文件,将其更改为字节数组,然后将其保存到硬盘中。
因此,我意识到此时我已经打开了文件,因此无需再次打开它。所以我做了一些修改(这次是整个课程):
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
d = {'Name': ['Bob', 'John', 'Alice'], 'Age': [25, 41, 30], 'Result' : [1.2, 0.5, 0.3], 'Ok' : [True, False, True]}
df = pd.DataFrame(data=d)
df.info()
print(df)
data = df.values
print(data)
pca = PCA(n_components=all)
pca.fit(data)
写入文件,但不使用UpdateCell()要求的数据输入来更新文件。
答案 0 :(得分:0)
好的...。这就是我最后得到的。它可能不漂亮,但是有效。
首先,我更改了click事件,以将更新与模板文件的传输分开。
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();
}
}