C ා implement excel operation — add tab sheet

Time:2020-7-11

C ා implements the operation on Excel, which will be placed in different tabs according to different types or sources of data. The code of tab added by C ා implementation is as follows: (path is the address where the document is saved, DT is the source data to be processed)

public void addSheet(string Path, DataTable dt)
    {
        var SlDoc = new SLDocument(Path);
        SlDoc.AddWorksheet ("SHEET2"); // add a new sheet

        Adddata (sldoc, DT); // fill in data

        SlDoc.SaveAs(Path);
    }
    Public void adddata (sldocument, sldoc, datatable, DT) // block data for each column of Excel
    {
        var strCell = Columns(dt);

        for (int j = 0; j < dt.Columns.Count; j++)
        {
            SlDoc.SetCellValue (strCell[j] + 1,  dt.Columns [j] . tostring()); // insert column name first
        }
        if (dt != null && dt.Rows.Count > 0)
        {
            //Data in the table checked by Excel by row
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                for (int i = 0; i < strCell.Length; i++)
                    SlDoc.SetCellValue(strCell[i] + (j + 2), dt.Rows[j][i].ToString());
            }
        }
    }
  Public string [] columns (datatable DT) // get excel column names a, B, C of each cell
    {
        var num = 65;
        var str = new string[dt.Columns.Count];
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            if (i < 26)
            {
                str[i] = ((char)num).ToString();
            }
            else
            {
                var aa = (i / 26) + 64;

                var bb = num - (i / 26) * 26;
                str[i] = ((char)aa).ToString() + ((char)bb).ToString();
            }