2014年12月31日 星期三

[.NET] GridVIew合併儲存格

說明:想讓GridView的某欄合併儲存格(非動態等資料繫結後,再判斷資料是否相符),該怎麼做?


做法:
在GridView的PreRender(GridView載入後與render前)事件中加上:

foreach (GridViewRow gvRow in GridView1.Rows)
        {
            if (gvRow.RowIndex > 0)//DataRow
            {
                //let column index+1 merge cell and assign the rows to across
                GridView1.Rows[0].Cells[index].RowSpan = GridView1.Rows.Count;
                //hide the extra cells
                gvRow.Cells[index].Visible = false;
            }
        }



參考資料:
http://www.dotblogs.com.tw/mis2000lab/archive/2008/04/archive/2008/04/archive/2008/04/24/3452.aspx
http://pramaire.pixnet.net/blog/post/31837470-gridview%E8%B3%87%E6%96%99%E5%88%97row%E5%90%88%E4%BD%B5%E6%AC%84%E4%BD%8D

2014年11月24日 星期一

[ClosedXML] 使用語法紀錄

using ClosedXML.Excel;

var workbook = new XLWorkbook(templateFilePath);
var worksheet = workbook.Worksheets.Worksheet(1);

//set cell value
worksheet.Cell(Row, Col).Value = string;

//set value by using DataTable
for (int j = 0; j < Data.Rows.Count; j++)
{
    for (int k = 0; k < Data.Columns.Count; k++)
    {
          worksheet.Cell(j, k).Value = Data.Rows[j][k].ToString();
     }
}

//set cell value in center
worksheet.Cell(R, C).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
worksheet.Cell(R, C).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;

//set border line
worksheet.Cell(R, C).Style.Border.SetRightBorder(XLBorderStyleValues.Thin);
worksheet.Cell(R, C).Style.Border.SetTopBorder(XLBorderStyleValues.Thin);
worksheet.Cell(R, C).Style.Border.SetBottomBorder(XLBorderStyleValues.Thin);
worksheet.Cell(R, C).Style.Border.SetLeftBorder(XLBorderStyleValues.Thin);

//column fit
worksheet.Column(columnIndex).AdjustToContents();
worksheet.Column(columnIndex).Width -= 1;

//merging columns
//Merge(true) might cause files error?
worksheet.Range(R, C, R2, C2).Column(1).Merge();

//copying worksheets
worksheet.CopyTo(sheetName);
worksheet.CopyTo(workbook2, sheetName);


workbook.SaveAs(filePath);


參考資料:
http://closedxml.codeplex.com/documentation
https://closedxml.codeplex.com/wikipage?title=Copying%20Worksheets
https://closedxml.codeplex.com/wikipage?title=Merging%20Cells

2014年11月6日 星期四

[Oracle] 在小於1的小數的整數位補0

說明:使用Round函式算百分比到小數位數,可能出現小於1的小數,Oracle不會顯示整數位數的0,如.03,那該如何補0呢?

做法:
使用TO_CHAR函式來將數值轉換成特定字串,語法:TO_CHAR(numeric, text),例:
--轉換為百分比,四捨五入到小數第2位,其中fm可去除首尾的空字元
SELECT TO_CHAR(0.05, 'fm990.09')||'%' FROM DUAL;


參考資料:
http://blog.csdn.net/hj402555749/article/details/8878587

2014年10月1日 星期三

[Oracle] 'OraOLEDB.Oracle' 提供者並未登錄於本機電腦上

說明:先安裝Win32 Oracle 11g R2 client,再安裝Win64 11g R2 client,Toad跟PL/SQL Developer都可存取DB,但執行本機程式卻出現「'OraOLEDB.Oracle' 提供者並未登錄於本機電腦上」的錯誤訊息。


嘗試方法:
1. 解除安裝Oracle,可參考此篇http://iamsbc.blogspot.com/2014/05/oracle-windowsoracle.html。
2. 先裝Win32 11g R2 client。
3. 再裝Win64 11g R2 client。

重裝一遍,程式就可以連了,順序都一樣,猜測可能是第一次安裝不完全吧!
P.S. 但第兩次是裝完整版


參考資料:
https://www.ptt.cc/bbs/Database/M.1393220172.A.E90.html

2014年9月25日 星期四

[.NET] 使用Microsoft.Office.Interop.Excel元件將資料匯出Excel

說明:有許多元件可將資料匯成Excel,這裡使用Excel 2010 PIA來實作,加入參考 -> 選.NET tab -> Microsoft.Office.Interop.Excel 12.0.0.0,namespace為Microsoft.Office.Interop.Excel。


做法:
//避免與Windows.Form.Application衝突
using Excel =  Microsoft.Office.Interop.Excel;

Excel.Application _Excel = null; //起一個Excel.exe
Excel.Workbook _Workbook = null; //活頁簿
Excel.Worksheet _Worksheet1 = null;
Excel.Sheets _Sheets = null;

string strTemplateFilePath = Application.StartupPath + @"\EXCEL\Template.xlsx"; //範本路徑
string strRptPath = Application.StartupPath + @"\EXCEL\Rpt\";
string strFilePath = string.Empty;
string strFileName = string.Empty;
           
try
{
    _Excel = new Excel.Application();
    _Excel.Visible = false;
    _Workbook = _Excel.Workbooks.Open(strTemplateFilePath);
    _Sheets = _Workbook.Worksheets;
    string[,] strTable;
 
    if (有資料)
    {
        _Worksheet1 = _Sheets.get_Item(1);
        _Worksheet1.Name = DateTime.Now.ToString("yyyyMM");

        int iInitialRow = 0; //起始列
        int iInitialCol = 0; //起始欄
        int iRowCnt = 資料來源筆數;
        int iColCnt = 資料來源欄位數;

        for (int x = 0; x < iRowCnt; x++)
        {
            for (int y = 0; y < iColCnt; y++)
            {
                strTable[x, y] = dt.Rows[x][y].ToString();
            }
        }

         Excel.Range _RangeStart = _Worksheet1.Cells[iInitialRow, iInitialCol];
         Excel.Range _RangeEnd = _Worksheet1.Cells[iInitialRow+iRowCnt, iInitialCol+iColCnt];
         Excel.Range _Range = (Excel.Range)_Worksheet1.get_Range(_RangeStart, _RangeEnd);
       
         _Range.Value2 = strTable;
         _Range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
         _Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
         _Range.Font.Name = "Times New Roman";
         _Range.Font.Size = 10;
         _Range.EntireColumn.AutoFit();
         _Range.Borders.Weight = Excel.XlBorderWeight.xlHairline;

         _Workbook.Application.DisplayAlerts = false;
         _Workbook.Application.AlertBeforeOverwriting = false;
         _Workbook.Saved = true;

         strFileName = "Rpt.xlsx";
         strFilePath = strRptPath + strFileName;
         _Workbook.SaveCopyAs(strFilePath);
    }
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    if (_Range != null)
   {
        Marshal.FinalReleaseComObject(_Range );
    }
    if (_Sheets != null)
    {
        Marshal.FinalReleaseComObject(_Sheets);
    }
    if (_Workbook != null)
    {
        _Workbook.Close(false);
        Marshal.FinalReleaseComObject(_Workbook);
     }
     if (_Excel != null)
     {
         _Excel.Workbooks.Close();
         _Excel.Quit();
         Marshal.FinalReleaseComObject(_Excel);
     }
}

 _Range.Cells.Text; //取得範本上資料
 //合併儲存格
 Excel.Range _RangeStart = _Worksheet1.Cells[int, int];
 Excel.Range _RangeEnd = _Worksheet1.Cells[int, int];
 _Worksheet1.get_Range(_RangeStart, _RangeEnd).Merge(0);
//新增工作表
_Worksheet1 = (Excel.Worksheet)_Workbook.Worksheets.Add(After: (Excel.Worksheet)_Workbook.Sheets[test], Count: 1);
_Worksheet1.Name = test+"_"+k;


參考資料:
http://www.dotblogs.com.tw/yc421206/archive/2012/03/09/70624.aspx
http://www.dotblogs.com.tw/yc421206/archive/2008/12/20/6470.aspx //fill in data
http://blog.darkthread.net/post-2013-05-14-excel-interop-notes.aspx //release
http://www.dotblogs.com.tw/chou/archive/2013/03/26/99016.aspx //Excel.Range.Cells.Text
http://fecbob.pixnet.net/blog/post/38189181-c%23-excel-%E8%A1%8C%E9%AB%98%E3%80%81%E5%88%97%E5%AF%AC%E3%80%81%E5%90%88%E4%BD%B5%E5%84%B2%E5%AD%98%E6%A0%BC%E3%80%81%E5%84%B2%E5%AD%98%E6%A0%BC%E9%82%8A%E6%A1%86 //Excel語法彙集
http://ww0o0ww.pixnet.net/blog/post/64084894-%E3%80%90c%23%E3%80%91%E3%80%8Amicrosoft.office.interop.excel%E3%80%8B%E5%AF%AB%E5%85%A5excel%E6%AA%94

2014年9月21日 星期日

[Reporting Service] 使用CDate函式轉換Date Type為YYYYMMDD

說明:使用Reporting Service繫結Oracle或SQL Server資料庫欄位為Date Type資料時,會出現「上午12:00:00」,該如何去除呢?


做法:
在Reporting Service繫結的欄位 -> properties(找屬性視窗) -> Value -> =Format(CDate(Fields!DATE.Value), "yyyy/MM/dd")


參考資料:
http://stackoverflow.com/questions/9722516/report-builder-3-0-how-to-convert-string-representation-of-dates-in-mm-dd-yy-fo

2014年8月24日 星期日

[UltraEdit] 使用區塊模式(column mode)替一堆字串一次加入單引號

說明:SQL使用IN查詢一堆同一欄位的資料,這些查詢的value需要單引號包起來,資料一多該怎麼處理比較方便呢?


作法:
使用UltraEdit的區塊模式(column mode),功能列->直行->區塊模式,快捷鍵為alt + c,轉換該模式後,將游標移到第一行字串前,按住shift不放再按滑鼠左鍵往下拖曳,此時會出現一條線,按下單引號鍵即可。


參考資料: