NPOI Excel
服务端导出
public virtual async Task<FileResult> DownLoad([FromQuery] XXXDto inputDto)
{
byte[] content;
string fileName;
using(FileStream fs = new FileStream("xxx.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook hssfworkbook = new XSSFWorkbook(fs);
ISheet sheet = hssfworkbook.GetSheetAt(0);
IRow rowMainTitle = sheet.GetRow(0);
IRow rowTitle = sheet.GetRow(1);
int startRowInx = 3;
int tableIndex = 0;
List<CellRangeAddress> cellRangeAddresses = new List<CellRangeAddress>();
foreach (var table in dataTables)
{
if (startRowInx != 3)
{
sheet.ShiftRows(startRowInx, sheet.LastRowNum, 1);
sheet.CopyRow(1, startRowInx++);
}
sheet.GetRow(startRowInx - 2).GetCell(0).SetCellValue(table[tableIndex].DisplayName + "应承担装卸费");
foreach (var it in table)
{
row.GetCell(0).SetCellValue(it.XXXX);
row.GetCell(1).SetCellValue(it.XXXX);
}
cellRangeAddresses.Add(new CellRangeAddress(_currentStartRowInx, startRowInx - 2, 0, 0));
}
cellRangeAddresses.ForEach(it =>
{
sheet.AddMergedRegion(it);
});
using (MemoryStream ms = new MemoryStream())
{
hssfworkbook.Write(ms);
content = ms.ToArray();
}
}
return File(content, "application/octet-stream", "export.xlsx");
}
前端下载
var formfield = form.val('form_wllaborrecord');
$.extend(formfield, { StatisticType: 'year' });
ajaxmod.requestAuthorize({
url: '/wllaborrecord/Salary/Statistic/Export',
type: 'Get',
data: formfield,
responseType:'arraybuffer',
success: function (response) {
var blob = new Blob([response], { type: 'application/vnd.ms-excel' });
var a = document.createElement('a');
a.download = '123.xlsx';
a.href = URL.createObjectURL(blob);
a.click();
}
});
var formfield = form.val('form_wllaborrecord');
$.extend(formfield, { StatisticType: 'year' });
var url = '/api/v1/wllaborrecord/Salary/Statistic/Export';
var inputs = '';
for (var key in formfield) {
inputs += '<input type="hidden" name="' + key + '" value="' + formfield[key] + '" />';
}
$('<form action="' + url + '" method=get">' + inputs + '</form>').appendTo('body').submit().remove();