foreach (var item in bh)
{
worksheet.Cells[rowNumber, 1].Value = item.bill_number;
worksheet.Cells[rowNumber, 2].Value = item.yearref + @"\" + item.monthref;
worksheet.Cells[rowNumber, 3].Value = item.stdate.ToShortDateString();
worksheet.Cells[rowNumber, 4].Value = item.enddate.ToShortDateString();
worksheet.Cells[rowNumber, 5].Value = item.duration;
worksheet.Cells[rowNumber, 6].Value = item.last_reading;
worksheet.Cells[rowNumber, 7].Value = item.new_reading;
worksheet.Cells[rowNumber, 8].Value = item.usage;
worksheet.Cells[rowNumber, 9].Value = item.totaldueamt;
worksheet.Cells[rowNumber, 10].Value = item.total_paid;
worksheet.Cells[rowNumber, 11].Value = item.paid_date.ToShortDateString(); }
我有上面的代码循环一个SQL结果集,后来用于导出到Excel,当item.paid_date为null时,在导出的Excel工作表中,日期显示为01/01/1900,我该怎么办?如果date为null,则将item.paid_date替换为''
这是商店程序,有时在阅读表中没有where bg.bill_number = p.bill_number
CREATE PROCEDURE [dbo].[BillHistory] @accNo varchar(50) as begin select bg.Id, bg.account_number, bg.bill_number, bg.year, bg.month, bg.usagetotal, bg.fineamt, bg.fuelcharge, bg.othercharge, bg.totalamt, bg.totaldueamt, bg.billdate, bg.bill_due_date, bg.outstanding_amt, bg.[desc], readingId = re.Id, re.accountno, re.oldccountNo, re.new_reading, re.last_reading, re.meterno, re.yearref , re.monthref , re.referencecount, re.stdate, re.enddate, current_reading_date = re.created_on, pre_reading_date = isnull((select top 1 c.created_on from reading c where c.accountno = @accNo and c.Id < re.Id order by c.Id desc),CAST(-53690 as datetime)), re.duration, re.usage, re.averageusage, re.averageconsumption, isnull((select p.total_paid from payment p where bg.bill_number = p.bill_number ),0) as total_paid, isnull((select p.created_on from payment p where bg.bill_number = p.bill_number ),0) as paid_date from reading re join billing_gen bg on re.Id=bg.reading_id where re.accountno = @accNo and bg.enable=1 order by bg.Id asc; end;
答案 0 :(得分:0)
你试过了吗?
worksheet.Cells[rowNumber, 11].Value = item.paid_date == null ? string.Empty : item.paid_date.ToShortDateString();