2014年7月30日 星期三

RDLC Invalid column

RDLC Invalid column

開發一支新的RDLC Report後,移至新主機上出現Invalid column的訊息,
最後原來是在新主機上的資料庫忘記開欄位


2014年7月21日 星期一

ASP.NET DataTable轉成Excel實體檔案

ASP.NET DataTable轉成Excel實體檔案


C#:
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. #region 建立虛擬表格
  4. DataTable dt = new DataTable();
  5. DataColumn column;
  6. column = new DataColumn();
  7. column.DataType = Type.GetType("System.String");
  8. column.ColumnName = "company_name";
  9. dt.Columns.Add(column);
  10. column = new DataColumn();
  11. column.DataType = Type.GetType("System.String");
  12. column.ColumnName = "tel";
  13. dt.Columns.Add(column);
  14. column = new DataColumn();
  15. column.DataType = Type.GetType("System.String");
  16. column.ColumnName = "fax";
  17. dt.Columns.Add(column);
  18. column = new DataColumn();
  19. column.DataType = Type.GetType("System.String");
  20. column.ColumnName = "email";
  21. dt.Columns.Add(column);
  22. column = new DataColumn();
  23. column.DataType = Type.GetType("System.String");
  24. column.ColumnName = "contact";
  25. dt.Columns.Add(column);
  26. column = new DataColumn();
  27. column.DataType = Type.GetType("System.String");
  28. column.ColumnName = "company_address";
  29. dt.Columns.Add(column);
  30. #endregion
  31.  
  32. //輸出
  33. ExcelFileStream(dt, @"D:\", "abc.xls");
  34. }
  1. private void ExcelFileStream(DataTable dt, string savepath, string filename)
  2. {
  3. try
  4. {
  5. Stream fs = RenderDataTableToExcel(dt);
  6. SaveStreamToFile(savepath + filename, fs);
  7. }
  8. catch (Exception)
  9. {
  10. throw;
  11. }
  12. }
  1. public static Stream RenderDataTableToExcel(DataTable srcTable)
  2. {
  3. try
  4. {
  5. HSSFWorkbook workbook = new HSSFWorkbook();
  6. HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
  7. HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
  8. foreach (DataColumn column in srcTable.Columns)
  9. {
  10. headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
  11. }
  12. int rowIndex = 1;
  13. foreach (DataRow row in srcTable.Rows)
  14. {
  15. HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
  16. foreach (DataColumn column in srcTable.Columns)
  17. {
  18. dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
  19. }
  20. rowIndex++;
  21. }
  22.  
  23. MemoryStream stream = new MemoryStream();
  24. workbook.Write(stream);
  25. stream.Flush();
  26. stream.Position = 0;
  27. sheet = null;
  28. headerRow = null;
  29. workbook = null;
  30. return stream;
  31. }
  32. catch (Exception)
  33. {
  34.  
  35. throw;
  36. }
  37.  
  38. }
  1. public void SaveStreamToFile(string fileFullPath, Stream stream)
  2. {
  3. try
  4. {
  5. if (stream.Length == 0) return;
  6.  
  7. // Create a FileStream object to write a stream to a file
  8. using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
  9. {
  10. // Fill the bytes[] array with the stream data
  11. byte[] bytesInStream = new byte[stream.Length];
  12. stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
  13.  
  14. // Use FileStream object to write to the specified file
  15. fileStream.Write(bytesInStream, 0, bytesInStream.Length);
  16. }
  17. }
  18. catch (Exception)
  19. {
  20. throw;
  21. }
  22.  
  23. }

2014年7月17日 星期四

ASP.NET 呼叫CMD執行批次檔BAT

ASP.NET 呼叫CMD執行批次檔BAT

C#:
  1. private void go()
  2. {
  3. try
  4. {
  5. // Get the full file path
  6. string strFilePath = @"C:\inetpub\wwwroot\ga\copyphone.bat";
  7.  
  8. // Create the ProcessInfo object
  9. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
  10. psi.UseShellExecute = false;
  11. psi.RedirectStandardOutput = true;
  12. psi.RedirectStandardInput = true;
  13. psi.RedirectStandardError = true;
  14. psi.WorkingDirectory = @"C:\inetpub\wwwroot\ga\";
  15.  
  16. // Start the process
  17. System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
  18.  
  19.  
  20. // Open the batch file for reading
  21. System.IO.StreamReader strm = new System.IO.StreamReader(strFilePath, System.Text.Encoding.GetEncoding(950));
  22. //System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
  23. //strm.CurrentEncoding.GetEncoder = System.Text.Encoding.GetEncoding(950);
  24. //strm = System.IO.File.OpenText(strFilePath);
  25. Response.Write(strm.CurrentEncoding.ToString());
  26.  
  27. // Attach the output for reading
  28. System.IO.StreamReader sOut = proc.StandardOutput;
  29.  
  30. // Attach the in for writing
  31. System.IO.StreamWriter sIn = proc.StandardInput;
  32.  
  33.  
  34. // Write each line of the batch file to standard input
  35. while (strm.Peek() != -1)
  36. {
  37. sIn.WriteLine(strm.ReadLine());
  38. }
  39.  
  40. strm.Close();
  41.  
  42. // Exit CMD.EXE
  43. string stEchoFmt = "# {0} run successfully. Exiting";
  44.  
  45. sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
  46. sIn.WriteLine("EXIT");
  47.  
  48. // Close the process
  49. proc.Close();
  50.  
  51. // Read the sOut to a string.
  52. string results = sOut.ReadToEnd().Trim();
  53.  
  54.  
  55. // Close the io Streams;
  56. sIn.Close();
  57. sOut.Close();
  58.  
  59. string fmtStdOut = "{0}";
  60. this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "
  61. ")));
  62. }
  63. catch (Exception ex)
  64. {
  65.  
  66. throw ex;
  67. }
  68. }

2014年7月2日 星期三

ASP.NET UseSubmitBehavior用法

ASP.NET UseSubmitBehavior用法

以下是我是目前的理解:有錯誤請指正

(1)一般用法:按Button1後執行的動作是Submit,執行Button1_Click,此時預設值UseSubmitBehavior="true"
  1. <asp:Button ID="Button1" runat="server" Text="Submit"  OnClick="Button1_Click" />

(2)加入UseSubmitBehavior="false"用法:按Button1後執行的動作是一般的Button,執行Button1_Click
  1. <asp:Button ID="Button1" runat="server" Text="Submit"  OnClick="Button1_Click" UseSubmitBehavior="false" />

(3)加入UseSubmitBehavior="false"和OnClientClick用法:按Button1後執行的動作是一般的Button,先執行OnClientClick後再執行Button1_Click
  1. <asp:Button ID="Button1" runat="server" Text="Submit"  OnClick="Button1_Click" UseSubmitBehavior="false" OnClientClick="this.disabled=true;this.value='Saveing...';" />

(4)OnClientClick用法:按Button1後執行的動作是一般的Button,先執行OnClientClick後再執行Button1_Click,如果在OnClientClick事件發生後卻得到false的訊息,就不會發生PostBack
  1. <asp:Button ID="Button1" runat="server" Text="Submit"  OnClick="Button1_Click" OnClientClick="return confirm('Are you sure you want to submit this form?');" />