.net inne

Set Data Format in Excel Using POI 3.0

The following is a list of built-in formats provided is POI 3.0:

S/No.

Format
Vlaue S/No
NPOI

Format
Value
1 General 0 12 h:mm AM/PM 0x12
2 0 1 13 h:mm:ss AM/PM 0x13
3 0.00 2 14 h:mm 0x14
4 #,##0 3 15 h:mm:ss 0x15
5 #,##0.00 4 16 m/d/yy h:mm 0x16
6 ($#,##0_);($#,##0) 5 17 (#,##0_);[Red](#,##0) 0x26
7 ($#,##0_);[Red]($#,##0) 6 18 (#,##0.00_);(#,##0.00) 0x27
8 ($#,##0.00);($#,##0.00) 7 19 (#,##0.00_);[Red](#,##0.00) 0x28
9 ($#,##0.00_);[Red]($#,##0.00) 8 20 _(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_) 0x29
10 0% 9 21 _($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_) 0x2a
11 0.00% 0xa 22 _(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_) 0x2b
12 0.00E+00 0xb 23 _($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_) 0x2c
13 # ?/? 0xc 24 mm:ss 0x2d
14 # ??/?? 0xd 25 [h]:mm:ss 0x2e
15 m/d/yy 0xe 26 mm:ss.0 0x2f
16 d-mmm-yy 0xf 27 ##0.0E+0 0x30
17 d-mmm 0x10 28 @-This is text format. 0x31
18 mmm-yy 0x11 29 text-Alias for "@" 0x31

Kategoria: 

NPOI – Copy Row Helper

http://www.zachhunter.com/2010/05/npoi-copy-row-helper/

/// <summary>
/// HSSFRow Copy Command
///
/// Description:  Inserts a existing row into a new row, will automatically push down
///               any existing rows.  Copy is done cell by cell and supports, and the
///               command tries to copy all properties available (style, merged cells, values, etc...)
/// </summary>
/// <param name="workbook">Workbook containing the worksheet that will be changed</param>
/// <param name="worksheet">WorkSheet containing rows to be copied</param>
/// <param name="sourceRowNum">Source Row Number</param>
/// <param name="destinationRowNum">Destination Row Number</param>
private void CopyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum, int destinationRowNum)
{
    // Get the source / new row
    HSSFRow newRow = worksheet.GetRow(destinationRowNum);
    HSSFRow sourceRow = worksheet.GetRow(sourceRowNum);
 
    // If the row exist in destination, push down all rows by 1 else create a new row
    if (newRow != null)
    {
        worksheet.ShiftRows(destinationRowNum, worksheet.LastRowNum, 1);
    }
    else
    {
        newRow = worksheet.CreateRow(destinationRowNum);
    }
 
    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.LastCellNum; i++)
    {
        // Grab a copy of the old/new cell
        HSSFCell oldCell = sourceRow.GetCell(i);
        HSSFCell newCell = newRow.CreateCell(i);
 
        // If the old cell is null jump to next cell
        if (oldCell == null)
        {
            newCell = null;
            continue;
        }
 
        // Copy style from old cell and apply to new cell
        HSSFCellStyle newCellStyle = workbook.CreateCellStyle();
        newCellStyle.CloneStyleFrom(oldCell.CellStyle); ;
        newCell.CellStyle = newCellStyle;
 
        // If there is a cell comment, copy
        if (newCell.CellComment != null) newCell.CellComment = oldCell.CellComment;
 
        // If there is a cell hyperlink, copy
        if (oldCell.Hyperlink != null) newCell.Hyperlink = oldCell.Hyperlink;
 
        // Set the cell data type
        newCell.SetCellType(oldCell.CellType);
 
        // Set the cell data value
        switch (oldCell.CellType)
        {
            case HSSFCellType.BLANK:
                newCell.SetCellValue(oldCell.StringCellValue);
                break;
            case HSSFCellType.BOOLEAN:
                newCell.SetCellValue(oldCell.BooleanCellValue);
                break;
            case HSSFCellType.ERROR:
                newCell.SetCellErrorValue(oldCell.ErrorCellValue);
                break;
            case HSSFCellType.FORMULA:
                newCell.SetCellFormula(oldCell.CellFormula);
                break;
            case HSSFCellType.NUMERIC:
                newCell.SetCellValue(oldCell.NumericCellValue);
                break;
            case HSSFCellType.STRING:
                newCell.SetCellValue(oldCell.RichStringCellValue);
                break;
            case HSSFCellType.Unknown:
                newCell.SetCellValue(oldCell.StringCellValue);
                break;
        }
    }
 
    // If there are are any merged regions in the source row, copy to new row
    for (int i = 0; i < worksheet.NumMergedRegions; i++)
    {
        CellRangeAddress cellRangeAddress = worksheet.GetMergedRegion(i);
        if (cellRangeAddress.FirstRow == sourceRow.RowNum)
        {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.RowNum,
                                                                        (newRow.RowNum +
                                                                         (cellRangeAddress.FirstRow -
                                                                          cellRangeAddress.LastRow)),
                                                                        cellRangeAddress.FirstColumn,
                                                                        cellRangeAddress.LastColumn);
            worksheet.AddMergedRegion(newCellRangeAddress);
        }
    }
 
}
 
 
 
// Grab my NPOI workbook memorystream
HSSFWorkbook workbook = new HSSFWorkbook(memoryStream);
 
// Grab my test worksheet
HSSFSheet sheet = workbook.GetSheet("Sheet1");
 
// Copy Excel Row 1 to Excel Row 3
CopyRow(workbook, sheet, 0, 2);
 
// Copy Excel Row 2 to Excel Row 4
CopyRow(workbook, sheet, 1, 3);
Kategoria: 

Strony

Subskrybuj .net inne