I have a WPF program that can export data from SQlite
ExportExcel = new RelayCommand(x =>
{
MessageBoxResult result = MessageBox.Show("Do you want to export to Excel?", "Export to Excel", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
var saveFileDialog = new SaveFileDialog
{
Filter = "Excel files|*.xlsx",
Title = "Save an Excel File"
};
saveFileDialog.ShowDialog();
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
for (int i = 0; i < _listCheckRecordModel.Count; i )
{
int cellIndex = i 1;
worksheet.Cell("A" cellIndex).Value = _listCheckRecordModel[i].Vender.VenderName;
worksheet.Cell("B" cellIndex).Value = _listCheckRecordModel[i].IssueDate;
worksheet.Cell("C" cellIndex).Value = _listCheckRecordModel[i].Amount;
worksheet.Cell("D" cellIndex).Value = _listCheckRecordModel[i].Drawer;
worksheet.Cell("E" cellIndex).Value = _listCheckRecordModel[i].BankTemplate;
}
workbook.SaveAs("HelloWorld.xlsx");
if (!String.IsNullOrWhiteSpace(saveFileDialog.FileName))
workbook.SaveAs(saveFileDialog.FileName);
}
});
But when it exports, it comes like this:
I want to name column A as 'Vender Name', B as 'Date', C as 'Amount' and so on when the user exports from WPF to excel. I searched online, but I couldn't find any help. How can I go about doing it?
Thanks in advance))
CodePudding user response:
I think it is not possible to rename excel column name. all you can do is to put header on the first row.
Try this, I don't know if it will work since i can't try it myself.
ExportExcel = new RelayCommand(x =>
{
MessageBoxResult result = MessageBox.Show("Do you want to export to Excel?", "Export to Excel", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
var saveFileDialog = new SaveFileDialog
{
Filter = "Excel files|*.xlsx",
Title = "Save an Excel File"
};
saveFileDialog.ShowDialog();
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A" 1).Value = "Vender Name";
worksheet.Cell("B" 1).Value = "Date";
worksheet.Cell("C" 1).Value = "Amount";
worksheet.Cell("D" 1).Value = "Drawer";
worksheet.Cell("E" 1).Value = "BankTemplate";
int cellIndex = 2;
for (int i = 0; i < _listCheckRecordModel.Count; i )
{
worksheet.Cell("A" cellIndex).Value = _listCheckRecordModel[i].Vender.VenderName;
worksheet.Cell("B" cellIndex).Value = _listCheckRecordModel[i].IssueDate;
worksheet.Cell("C" cellIndex).Value = _listCheckRecordModel[i].Amount;
worksheet.Cell("D" cellIndex).Value = _listCheckRecordModel[i].Drawer;
worksheet.Cell("E" cellIndex).Value = _listCheckRecordModel[i].BankTemplate;
cellIndex ;
}
workbook.SaveAs("HelloWorld.xlsx");
if (!String.IsNullOrWhiteSpace(saveFileDialog.FileName))
workbook.SaveAs(saveFileDialog.FileName);
}
});