Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 2.54 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Exporting to Excel
  2. The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data
  3.        
  4. string lFilename = Leads.xls";
  5. string lDistributorFolder = Server.MapPath(".") + "\Portals\0\Distributors\" + _currentUser.UserID.ToString() + "\";
  6. string lTemplateFolder = System.Configuration.ConfigurationManager.AppSettings["Templates"];
  7. System.IO.Directory.CreateDirectory(lDistributorFolder);
  8.  
  9. File.Copy(lTemplateFolder + lFilename, lDistributorFolder + lFilename, true);
  10. string lConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lDistributorFolder + "\" + lFilename + ";Extended Properties="Excel 8.0;HDR=YES;"";
  11. DbProviderFactory lFactory = DbProviderFactories.GetFactory("System.Data.OleDb");
  12. int lSequence = 0;
  13.  
  14. using (DbConnection lConnection = lFactory.CreateConnection())
  15. {
  16.     lConnection.ConnectionString = lConnectionString;
  17.     lConnection.Open();
  18.  
  19. foreach (GridDataItem lItem in grdLeadList.Items)
  20.   {
  21.     lSequence++;
  22.  
  23.     using (DbCommand lCommand = lConnection.CreateCommand())
  24.       {
  25.            lCommand.CommandText = "INSERT INTO [ColderLeads$] ";
  26.            lCommand.CommandText += "(F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21) ";
  27.            lCommand.CommandText += "VALUES(";
  28.            lCommand.CommandText += """ + lSequence.ToString() + "",";
  29.            lCommand.CommandText += """ + lItem.Cells[_gLeadNumber].Text.Replace(" ", " ") + "",";
  30.            lCommand.CommandText += """ + lItem.Cells[_gSource].Text.Replace(" ", " ") + "",";
  31.            lCommand.CommandText += """ + lItem.Cells[_gAccountName].Text.Replace(" ", " ") + "",";
  32.            lCommand.CommandText += """ + lItem.Cells[_gCreatedOn].Text.Replace(" ", " ") + "",";
  33.            lCommand.CommandText += """ + lItem.Cells[_gContactFullName].Text.Replace(" ", " ") + "",";
  34.            lCommand.CommandText += """ + lItem.Cells[_gPriority].Text.Replace(" ", " ") + "",";
  35.            lCommand.CommandText += """ + lItem.Cells[_gStreet1].Text.Replace(" ", " ") + "",";
  36.            lCommand.CommandText += """ + lItem.Cells[_gStreet2].Text.Replace(" ", " ") + "",";
  37.            lCommand.CommandText += """ + lItem.Cells[_gZIP].Text.Replace(" ", " ") + "",";
  38.            lCommand.CommandText += """ + lItem.Cells[_gCity].Text.Replace(" ", " ") + "",";
  39.            lCommand.CommandText += """ + lItem.Cells[_gState].Text.Replace(" ", " ") + "",";
  40.            lCommand.CommandText += ")";
  41.            lCommand.ExecuteNonQuery();
  42.          }
  43.       }
  44.  
  45.      lConnection.Close();
  46.   }