我有一个创建Excelfile的方法
该文件的目录当前是“硬编码的”
反正是否让用户选择自己的下载路径?
没有用户将路径插入常规文本框?
这是我用来创建Excelfile
:
public void CreateExcelSheet(List<Calculation> cList, string sheetName, string fileName, FormCollection form, int DTCyear)
{
List<Calculation> newList = new List<Calculation>();
newList.Add(cList.First());
var StartValue = newList[0].StartValue;
DataTable dt = new DataTable();
dt = ConvertClass.ConvertToDataTable(newList);
var radio = form["advanceOrArrears"];
dt.Columns.Remove("StartValue");
dt.Columns.Remove("NumberOfDays");
dt.Columns.Remove("PeriodStartDate");
dt.Columns.Remove("PeriodEndDate");
string path = @"C:\ExcelFiles\" + fileName + ".xlsx";
FileInfo info = new FileInfo(path);
info.Directory.Create();
if (!info.Exists)
{
using (ExcelPackage package = new ExcelPackage(info))
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add(sheetName);
ws.Cells["A:A,B:B"].Style.Numberformat.Format = "yyyy-mm-dd";
ws.Cells["C:C,E:E,F:F,G:G"].Style.Numberformat.Format = "#.00";
ws.Cells["D:D"].Style.Numberformat.Format = "#.00000%";
ws.Column(1).Width = 16;
ws.Column(2).Width = 16;
ws.Column(3).Width = 16;
ws.Column(4).Width = 16;
ws.Column(5).Width = 16;
ws.Column(6).Width = 16;
ws.Column(7).Width = 16;
ws.Cells[5, 3].LoadFromDataTable(dt, false);
ws.Cells["A3"].Value = "Start date";
ws.Cells["B3"].Value = "End date";
ws.Cells["C3"].Value = "Invoice amount";
ws.Cells["D3"].Value = "Interest rate";
ws.Cells["E3"].Value = "Interest amount";
ws.Cells["F3"].Value = "Amortization";
ws.Cells["G3"].Value = "Capital balance";
ws.Cells["G4"].Value = StartValue;
ws.Cells["A5"].Formula = "=DATE(" + cList[0].PeriodStartDate.ToString("yyyy,M,d") + ")";
ws.Cells["B5"].Formula = "=DATE(" + cList[0].PeriodEndDate.ToString("yyyy,M,d") + ")";
var rowCount = 6;
var cListCount = 1;
var rowCount2 = 5;
for (var i = 0; i < Convert.ToInt32(form["PaymentPeriods"]) - 1; i++)
{
ws.Cells["A" + rowCount].Formula = "=DATE(" + cList[cListCount].PeriodStartDate.ToString("yyyy,M,d") + ")";
ws.Cells["B" + rowCount].Formula = "=DATE(" + cList[cListCount].PeriodEndDate.ToString("yyyy,M,d") + ")";
ws.Cells["C" + rowCount].Value = cList[cListCount].InvoiceAmount;
ws.Cells["D" + rowCount].Value = cList[cListCount].InterestRate;
if (radio == "inAdvance")
{
ws.Cells["E" + rowCount].Formula = "(" + ws.Cells[rowCount2, 7].Address + "-" + ws.Cells[rowCount, 3].Address + ")*" + ws.Cells[rowCount, 4].Address + "/" + DTCyear + "*" + cList[cListCount].NumberOfDays;
}
else
{
ws.Cells["E" + rowCount].Formula = "(" + ws.Cells[rowCount2, 7].Address + ")*" + ws.Cells[rowCount, 4].Address + "/" + DTCyear + "*" + cList[cListCount].NumberOfDays;
}
ws.Cells["F" + rowCount].Formula = ws.Cells[rowCount, 3].Address + "-" + ws.Cells[rowCount, 5].Address;
ws.Cells["G" + rowCount].Formula = ws.Cells[rowCount2, 7].Address + "-" + ws.Cells[rowCount, 6].Address;
rowCount++;
rowCount2++;
cListCount++;
}
using (ExcelRange range = ws.Cells["A3:G3"])
{
range.Style.Font.Bold = true;
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));
range.Style.Font.Color.SetColor(Color.White);
}
byte[] data = package.GetAsByteArray();
System.IO.File.WriteAllBytes(path, data);
}
}
}