第一步:将NPOI中的一下三个文件复制到项目中
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdwAAABCCAIAAABy2vAlAAAatklEQVR4nO2dd1xT5/7Hmy477+2v42p37bq1Q3tvh9XaW+1Sa3FURcVR66xFxQEKahEVN0vQClWWgCwZYYaZhEAYCSvskcFIIJCQkE0S8vz+SC83JSfJISRw0Of9el68jifPeZ5zzvP9fM43j+ec3HcyNAcWWGCBBRaMlPsABAKBQDADNGUIBALBENCUIRAIBENAU4ZAIBAMAU0ZAoFAMAQ0ZQgEAsEQCKYcQtPhG7Wp9erkWlVStTyxUppAG4wvG4grEdym9MWQe6OJ3FsFXZG5nPBsVlhmW2h66w180x/JDSF3GMEJNb/HVl2LoV2NKg+MoF4JKz5ylhCfVjvxBwaxLTAqIIaQSCQajdZQ39RQ3zL+UltTl52dXV1dPdmHhQkQTPl6qVq/oNPphoeHtVqtVqvVaDRqtVqtVg8NDalUKqVSqVQqFQqFXC6Xy+UymUwqlUokUvGgRCQeFAyI+4VivkDU2z/gdiYriy3YfTgkPAbfyuyQK1Q63cQeIsQWwKiAGKI3ZS6XJxYNjr90c/n0qsYrgVcJBMJkH9nkg2DK14qVurFLRAeAVgfUWqDSAKUGyNVAOgQGVcDVK61IDk7EcVzcw1IyClkcrkIJFTj1gFEBMYREItFo9IGBAZu0NihRsNi9pWUVR44coVKpYrHYJs1OURBMOahIPjw8PKZWTGlPrASHPFMJ/cCNBJwjWS5HbqZmEFmcbqjAKQeMCoghJBKJRqseGBDZpDWlaqivf6CqqppCoeDx+Ht8HgPBlANJUo1Gg74JM9obUICDJ5KTOOCXPLApG2y6wdx/OCQ1oxCNAu+77z74105/rQAjUQHBCLbNlDVarVQqq6+vr6lh1NU1BgeHpKWl2aTlqQiCRK8UiNVqNcrtzWtPIAcHPBKjGsGmbLA6A3ybCr4Patt74BpU4CRinS/DqIAYop9TtpUpAwBUKlVLS0tbG7NfIGpnsnbv2kWlUkUi22TiUwsEfQbkCoeGhtBsb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YN+Upy4I+ty27SqdTkepPUL72LQXXg0yKgGtAqxadRrKbwoBowJiCDRl+2HKlCV0OrBfCaGDlStPbdzhlplDhvKbEsCogBgCTdl+IJjy1q2BW7YEbN7sv2mT38aNvk5OPhs2XF637uLatefXrj2/Zs25H3/0XrXqjHVl5crTy5efcnDwWr781O4DJ3MLSkRiCZQf9oFRATGETCZXVFQIBAKt/RkaGiopKbmnTZleXe93NWKXi+fGHW5O213HWjZsO7z+50Mbth3WF8Q6G3e47XLx9LsaQa+ul0rlE3/YkLECowJiSHl5OR6PJ5PJVPtTUlKSkZFxT5tyb5+AXl2fR6Rm5pAzCCTrSjqBaObTzBxyHpFKr67v7ROoNaN/yBaCQWBUQAxhsViBgYG+E0hWVtZkH/QEgWDKao1GKpWLxBLhgNjqIhCKzFcQiSVSqVyt0cBvqVMCGBUQyMQwJe8FhkAgkLsVaMoQCASCIaApQyAQCIaApgyBQCAYApoyBAKBYAhoyhAIBIIhoClDIBAIhoCmDIFAIBgCmjIEAoFgCGjKEAgEgiGgKUMgEAiG+H+JY/x1uhQlOQAAAABJRU5ErkJggg==" alt="" />
第二部在项目中为其添加引用,在右击引用,在浏览选项卡上。
在导出的按钮上添加CLlck事件。代码如下:
private void btnExportToExcel_Click(object sender, RoutedEventArgs e)
{ SaveFileDialog sdfExport = new SaveFileDialog();
sdfExport.Filter = "Excel文件|*.xls";
if (sdfExport.ShowDialog() != true)
{
return;
} string filename = sdfExport.FileName;
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("员工数据"); IRow rowHeader = sheet.CreateRow();//表头行
rowHeader.CreateCell(, CellType.STRING).SetCellValue("姓名");
rowHeader.CreateCell(, CellType.STRING).SetCellValue("工号");
rowHeader.CreateCell(, CellType.STRING).SetCellValue("入职日期"); //把查询结果导出到Excel
Employee[] employees = (Employee[])datagrid.ItemsSource;
for (int i = ; i < employees.Length; i++)
{
Employee employee = employees[i];
IRow row = sheet.CreateRow(i + );
row.CreateCell(, CellType.STRING).SetCellValue(employee.Name);
row.CreateCell(, CellType.STRING).SetCellValue(employee.Number); ICellStyle styledate = workbook.CreateCellStyle();
IDataFormat format = workbook.CreateDataFormat();
//格式具体有哪些请看单元格右键中的格式,有说明
styledate.DataFormat = format.GetFormat("yyyy\"年\"m\"月\"d\"日\""); ICell cellInDate = row.CreateCell(, CellType.NUMERIC);
cellInDate.CellStyle = styledate;
cellInDate.SetCellValue(employee.InDate);
} using (Stream stream = File.OpenWrite(filename))
{
workbook.Write(stream);
}
}