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#:
    protected void Button1_Click(object sender, EventArgs e)
    {
        #region 建立虛擬表格
        DataTable dt = new DataTable();
        DataColumn column;
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "company_name";
        dt.Columns.Add(column);
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "tel";
        dt.Columns.Add(column);
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "fax";
        dt.Columns.Add(column);
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "email";
        dt.Columns.Add(column);
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "contact";
        dt.Columns.Add(column);
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "company_address";
        dt.Columns.Add(column);
        #endregion

        //輸出
        ExcelFileStream(dt, @"D:\", "abc.xls");
    }
    private void ExcelFileStream(DataTable dt, string savepath, string filename)
    {
        try
        {
            Stream fs = RenderDataTableToExcel(dt);
            SaveStreamToFile(savepath + filename, fs);
        }
        catch (Exception)
        {
            throw;
        }
    }
    public static Stream RenderDataTableToExcel(DataTable srcTable)
    {
        try
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
            HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
            foreach (DataColumn column in srcTable.Columns)
            {
                headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
            }
            int rowIndex = 1;
            foreach (DataRow row in srcTable.Rows)
            {
                HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
                foreach (DataColumn column in srcTable.Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                }
                rowIndex++;
            }

            MemoryStream stream = new MemoryStream();
            workbook.Write(stream);
            stream.Flush();
            stream.Position = 0;
            sheet = null;
            headerRow = null;
            workbook = null;
            return stream;
        }
        catch (Exception)
        {

            throw;
        }

    }
    public void SaveStreamToFile(string fileFullPath, Stream stream)
    {
        try
        {
            if (stream.Length == 0) return;

            // Create a FileStream object to write a stream to a file
            using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
            {
                // Fill the bytes[] array with the stream data
                byte[] bytesInStream = new byte[stream.Length];
                stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

                // Use FileStream object to write to the specified file
                fileStream.Write(bytesInStream, 0, bytesInStream.Length);
            }
        }
        catch (Exception)
        {
            throw;
        }

    }

2014年7月17日 星期四

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

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

C#:
    private void go()
    {
        try
        {
            // Get the full file path
            string strFilePath = @"C:\inetpub\wwwroot\ga\copyphone.bat";

            // Create the ProcessInfo object
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.WorkingDirectory = @"C:\inetpub\wwwroot\ga\";

            // Start the process
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);


            // Open the batch file for reading
            System.IO.StreamReader strm = new System.IO.StreamReader(strFilePath, System.Text.Encoding.GetEncoding(950));
            //System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
            //strm.CurrentEncoding.GetEncoder = System.Text.Encoding.GetEncoding(950);
            //strm = System.IO.File.OpenText(strFilePath);
            Response.Write(strm.CurrentEncoding.ToString());

            // Attach the output for reading
            System.IO.StreamReader sOut = proc.StandardOutput;

            // Attach the in for writing
            System.IO.StreamWriter sIn = proc.StandardInput;


            // Write each line of the batch file to standard input
            while (strm.Peek() != -1)
            {
                sIn.WriteLine(strm.ReadLine());
            }

            strm.Close();

            // Exit CMD.EXE
            string stEchoFmt = "# {0} run successfully. Exiting";

            sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
            sIn.WriteLine("EXIT");

            // Close the process
            proc.Close();

            // Read the sOut to a string.
            string results = sOut.ReadToEnd().Trim();


            // Close the io Streams;
            sIn.Close();
            sOut.Close();

            string fmtStdOut = "{0}";
            this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "
"))); } catch (Exception ex) { throw ex; } }

2014年7月2日 星期三

ASP.NET UseSubmitBehavior用法

ASP.NET UseSubmitBehavior用法

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

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

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

(3)加入UseSubmitBehavior="false"和OnClientClick用法:按Button1後執行的動作是一般的Button,先執行OnClientClick後再執行Button1_Click
<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
<asp:Button ID="Button1" runat="server" Text="Submit"  OnClick="Button1_Click" OnClientClick="return confirm('Are you sure you want to submit this form?');" />