コード例 #1
0
 private void BtnExport_Click(object sender, System.EventArgs e)
 {
     if (DateExportFrom.Value > DateExportTo.Value)
     {
         MessageForm.Show("Export Invoice", "Please select from date less or equal to date.");
         return;
     }
     BusinessService.BusinessService service = new BusinessService.BusinessService();
     DataTable dTable = ReportConverter.Convert(service.ExportInvoice(DateExportFrom.Value, DateExportTo.Value));
     if (dTable == null)
     {
         MessageForm.Show("Export Invoice", "No invoice in range to export.");
         return;
     }
     // Create output file
     DateTime now = DateTime.Now;
     StringBuilder sb = new StringBuilder();
     CheckBillService.CheckBillService bService = new CheckBillService.CheckBillService();
     string path = bService.GetDescription("EXPORT_PATH");
     sb.Append(path);
     sb.Append("invoice_");
     sb.Append(now.ToString("yyyyMMddhhmmss"));
     sb.Append(".csv");
     StreamWriter sw = File.CreateText(sb.ToString());
     // Write Header
     sb.Length = 0;
     sb.Append("Export Invoice when ");
     sb.Append(now.ToString("dd/MM/yyyy hh:mm:ss"));
     sw.WriteLine(sb.ToString());
     sb.Length = 0;
     for (int i = 0;i < dTable.Columns.Count;i++)
     {
         if (i > 0)
             sb.Append(",\"");
         else
             sb.Append("\"");
         sb.Append(dTable.Columns[i].ColumnName);
         sb.Append("\"");
     }
     sw.WriteLine(sb.ToString());
     // Write Data
     for (int i = 0;i < dTable.Rows.Count;i++)
     {
         DataRow row = dTable.Rows[i];
         sb.Length = 0;
         for (int j = 0;j < row.ItemArray.Length;j++)
         {
             if (j > 0)
                 sb.Append(",");
             sb.Append(row[j].ToString());
         }
         sw.WriteLine(sb.ToString());
     }
     sw.Close();
     MessageForm.Show("Export Invoice", "Export Invoice Completed...");
 }