C# Excel操作示例

DataTable table = new DataTable();

字串8


using (SqlConnection conn = new SqlConnection(@"Server=.\SQLExpress;DataBase=CHSJ_FaceMatch;UID=sa;PWD=sa")) 字串3

{ 字串8

    conn.Open(); 字串1

    using (SqlCommand cmd = conn.CreateCommand()) 字串7

    { 字串5

        cmd.CommandText = "select top 10 IdentityID,Name,Birthday,NativePlace,TrainDataFile from T_Criminal"; 字串5

        table.Load(cmd.ExecuteReader());

字串3

    } 字串2

 

字串2

}

字串7


dataGridViewX1.DataSource = table; 字串6

//总列数 字串1

int columnCount = dataGridViewX1.Columns.Count; 字串8

//创建Excel对象 字串7

Excel.Application excelApp = new Excel.ApplicationClass();

字串7


//新建工作簿 字串1

Excel.Workbook workBook = excelApp.Workbooks.Add(true); 字串7

//新建工作表 字串9

Excel.Worksheet worksheet = workBook.ActiveSheet as Excel.Worksheet;

字串8

//设置标题

字串1

Excel.Range titleRange = worksheet.get_Range( 字串7

    worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]);    //选取单元格

字串9

titleRange.Merge(true);                                          //合并单元格 字串4

titleRange.Value2 = "犯罪人员信息";                              //设置单元格内文本 字串2

titleRange.Font.Name = "黑体";                                  //设置字体 字串8

titleRange.Font.Size = 20;                                      //字体大小

字串4


titleRange.Font.Bold = true;                                    //加粗显示

字串3


titleRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;  //水平居中 字串1

titleRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;    //垂直居中 字串6

titleRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;  //设置边框

字串6


titleRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;        //边框常规粗细 字串4

  字串3

//设置表头

字串5

for (int i = 0; i < columnCount; i++)

字串9

{ 字串8

    Excel.Range headRange = worksheet.Cells[2, i + 1] as Excel.Range; //获取表头单元格

字串2

    headRange.Value2 = dataGridViewX1.Columns[i].HeaderText;          //设置单元格文本

字串9

    headRange.Font.Name = "黑体";                                    //设置字体

字串5


    headRange.Font.Size = 14;                                        //字体大小

字串8


    headRange.Font.Bold = true;                                      //加粗显示 字串6

    headRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;    //水平居中 字串6

    headRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;      //垂直居中 字串5

    headRange.ColumnWidth = dataGridViewX1.Columns[i].Width / 8;      //设置列宽

字串1


    headRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;    //设置边框 字串9

    headRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;        //边框常规粗细 字串3

} 字串1

 

字串7

//填充数据

字串6

for (int i = 0; i < dataGridViewX1.Rows.Count; i++) 字串2

{ 字串7

    for (int j = 0; j < dataGridViewX1.Columns.Count; j++)

字串3

    {

字串9

        Excel.Range contentRange = worksheet.Cells[i + 3, j + 1] as Excel.Range; //获取单元格

字串9


        contentRange.Value2 = dataGridViewX1[j, i].Value;                        //设置单元格文本

字串9


        contentRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;        //设置边框 字串9

        contentRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;            //边框常规粗细 字串5

        contentRange.WrapText = true;                                            //自动换行 字串2

    }

字串9


}

字串4


 

字串9

//设置每列格式

字串3


//身份证列

字串6


Excel.Range identityIdRange = worksheet.get_Range(worksheet.Cells[3, 1], worksheet.Cells[dataGridViewX1.RowCount + 3, 1]); 字串7

identityIdRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;  //对其方式

字串6


identityIdRange.NumberFormatLocal = "0";                            //格式化文本 字串9

//生日列

字串5


Excel.Range birthdayRange = worksheet.get_Range(worksheet.Cells[3, 3], worksheet.Cells[dataGridViewX1.RowCount + 3, 3]); 字串2

birthdayRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //对其方式

字串1

birthdayRange.NumberFormatLocal = "yyyy-MM-dd";                    //格式化文本

字串6


 

字串4


//设置Excel可见

字串6

excelApp.Visible = true;

字串4

//显示打印预览 字串9

worksheet.PrintPreview(true); 字串7