Skip to content

Commit b36c0a4

Browse files
committed
download button for CSV
1 parent adfe641 commit b36c0a4

File tree

3 files changed

+67
-75
lines changed

3 files changed

+67
-75
lines changed

Pages/Export.razor

Lines changed: 54 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@inject IList<Fund> Funds
66
@inject HttpClient Http
77
@using Models
8+
@using System.Text
89

910
<PageTitle>@pageHeading</PageTitle>
1011

@@ -31,91 +32,66 @@
3132
<br/>
3233

3334
<div>
34-
@if (appData.FamilyData.Accounts.Count > 0)
35-
{
36-
<div>Broker,Account,TaxType,Type,Owner,Ticker,Shares,Price,Value,Investment,ExpenseRatio,InvestmentCategory,closeYest,AnnualFees,AccountOwnerAndType</div>
37-
}
38-
@{var rowCount = 1;}
39-
@foreach (var account in appData.FamilyData.Accounts.OrderBy(a => a.Custodian).ThenByDescending(a => a.Value))
35+
<button @onclick="DownloadCsv">Download CSV</button>
36+
</div>
37+
</div>
38+
39+
@code {
40+
private async Task DownloadCsv()
41+
{
42+
string? csvContent = GenerateCsv(); // Generate CSV content
43+
44+
if (csvContent != null)
45+
{
46+
// Call JavaScript function to download CSV
47+
await JS.InvokeVoidAsync("downloadFile", appData?.FamilyData?.AppData.CurrentProfileName + " multifolio.csv", csvContent);
48+
}
49+
}
50+
private string? GenerateCsv()
51+
{
52+
if (appData?.FamilyData?.Accounts.Count > 0)
53+
{
54+
var csv = new StringBuilder();
55+
csv.AppendLine("Broker,Account,TaxType,Type,Owner,Ticker,Shares,Price,Value,Investment,ExpenseRatio,InvestmentCategory,closeYest,AnnualFees,AccountOwnerAndType");
56+
var rowCount = 1;
57+
foreach (var account in appData.FamilyData.Accounts.OrderBy(a => a.Custodian).ThenByDescending(a => a.Value))
4058
{
4159
bool isTreasuryDirect = account.Custodian?.ToLowerInvariant() == "treasury direct" || account.Custodian?.ToLowerInvariant() == "treasurydirect";
4260
foreach (var investment in account.Investments.OrderByDescending(a => a.Value))
4361
{
4462
rowCount++;
45-
@* if (investment.Ticker == "IBOND" && !ibondHeaderShown)
46-
{
47-
ibondHeaderShown = true;
48-
<tr style=vertical-align:bottom>
49-
<td style=padding-left:8px>Holding</td>
50-
<td style=padding-left:8px;text-align:center>'Fixed'<br />Rate</td>
51-
<td style=padding-left:8px;text-align:center>Current<br />Rate</td>
52-
<td style=padding-left:8px;text-align:center>Balance</td>
53-
</tr>
54-
} *@
55-
<div>
56-
@account.Custodian,
57-
@(account.Note == null || account.Note == "" ?
63+
csv.AppendLine(
64+
account.Custodian + "," +
65+
(account.Note == null || account.Note == "" ?
5866
account.Identifier :
5967
account.Note
60-
),
61-
@account.TaxType2,
62-
@(account.AccountType == "Taxable" ? "Brokerage" : @account.AccountType),
63-
@account.Identifier,
64-
@(investment.Ticker != null ? investment.Ticker : investment.Name),
65-
@(investment.Ticker == "IBOND" ? @investment.Value / 25 : @investment.Shares),
66-
@(investment.InvestmentOrderCategory == "Cash" ? "1.0" :
67-
(@investment.Ticker == "IBOND" ? "25" : "=googlefinance(f"+@rowCount+")")),
68-
@("=g"+@rowCount+"*h"+@rowCount),
69-
@investment.Name,
70-
@investment.ExpenseRatio,
71-
@investment.InvestmentOrderCategory,
72-
@(investment.InvestmentOrderCategory == "Cash" ? "1.0" :
73-
investment.Ticker == "IBOND" ? "25" : "\"=googlefinance(f"+@rowCount+",\"\"closeyest\"\")\""
74-
),
75-
@("=K"+@rowCount+"*I"+@rowCount+"/100"),
76-
@("\"=CONCATENATE(E"+@rowCount+",IF(E"+@rowCount+"<>\"\"\"\",\"\" \"\",\"\"\"\"),D"+@rowCount+")\"")
77-
78-
</div>
79-
@* <td>
80-
@if (investment.IsIBond)
81-
{
82-
<span>&nbsp;</span>
83-
84-
@FormatUtilities.FormatMonthPlus2DigitYear(investment.PurchaseDate)
85-
}
86-
</td>
87-
@if (investment.IsIBond)
88-
{
89-
<td style=text-align:right>@FormatUtilities.FormatPercent3(investment.InterestRate)</td>
90-
<td style=text-align:right>@FormatUtilities.FormatPercent3(investment.CurrentRate)</td>
91-
}
92-
else
93-
{
94-
<td style=text-align:right;padding-left:8px>@FormatUtilities.FormatDoubleTwoDecimal(investment.Shares)</td>
95-
<td style=text-align:right;padding-left:8px>@FormatUtilities.FormatMoney(investment.Price)</td>
96-
}
97-
98-
<td style=text-align:right;padding-left:8px>
99-
@FormatUtilities.FormatMoney(investment.Value, decimalPlaces: 0)
100-
</td> *@
101-
@* if (investment.IsIBond && investment.NextRate.HasValue)
102-
{
103-
<tr style=background-color:@bgColor >
104-
<td colspan=2 style=text-align:right>
105-
as of @FormatUtilities.FormatMonthPlus2DigitYear(@investment.NextRateStart):
106-
</td>
107-
<td style=text-align:right>
108-
@FormatUtilities.FormatPercent3(investment.NextRate)
109-
</td>
110-
<td></td>
111-
</tr>
112-
} *@
68+
) + "," +
69+
account.TaxType2 + "," +
70+
(account.AccountType == "Taxable" ? "Brokerage" : @account.AccountType)+ "," +
71+
account.Identifier+ "," +
72+
(investment.Ticker != null ? investment.Ticker : investment.Name)+ "," +
73+
(investment.Ticker == "IBOND" ? @investment.Value / 25 : @investment.Shares)+ "," +
74+
(investment.InvestmentOrderCategory == "Cash" ? "1.0" :
75+
(investment.Ticker == "IBOND" ? "25" : "=googlefinance(f"+@rowCount+")")) + "," +
76+
("=g"+@rowCount+"*h"+@rowCount)+ "," +
77+
investment.Name+ "," +
78+
investment.ExpenseRatio+ "," +
79+
investment.InvestmentOrderCategory+ "," +
80+
(investment.InvestmentOrderCategory == "Cash" ? "1.0" :
81+
investment.Ticker == "IBOND" ? "25" : "\"=googlefinance(f"+@rowCount+",\"\"closeyest\"\")\""
82+
)+ "," +
83+
("=K"+@rowCount+"*I"+@rowCount+"/100")+ "," +
84+
("\"=CONCATENATE(E"+@rowCount+",IF(E"+@rowCount+"<>\"\"\"\",\"\" \"\",\"\"\"\"),D"+@rowCount+")\"")
85+
);
11386
}
11487
}
115-
</div>
116-
</div>
11788

118-
@code {
89+
return csv.ToString();
90+
}
91+
92+
return null;
93+
}
94+
11995
private async Task exportCSV()
12096
{
12197
if (appData?.FamilyData != null)
@@ -179,4 +155,7 @@
179155
private bool showError { get; set; }
180156
private string? pageHeading;
181157
private bool csvImport = false;
158+
159+
[Inject]
160+
private IJSRuntime JS { get; set; }
182161
}

wwwroot/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<a class="dismiss">🗙</a>
8585
</div>
8686
<script src="_framework/blazor.webassembly.js"></script>
87+
<script src="js/downloadFile.js"></script>
8788
<script>navigator.serviceWorker.register('service-worker.js');</script>
8889
</body>
8990

wwwroot/js/downloadFile.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function downloadFile(filename, content) {
2+
var element = document.createElement('a');
3+
element.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(content));
4+
element.setAttribute('download', filename);
5+
6+
element.style.display = 'none';
7+
document.body.appendChild(element);
8+
9+
element.click();
10+
11+
document.body.removeChild(element);
12+
}

0 commit comments

Comments
 (0)