/// <summary> /// 导出数据到本地 /// </summary> /// <param name="dt">要导出的数据</param> /// <param name="tableName">表格标题</param> /// <param name="path">保存路径</param> public void OutFileToDisk(DataGridView dgv, string path) { Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 sheet.Name = "属性"; Cells cells = sheet.Cells;//单元格 //为标题设置样式 Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 styleTitle.Font.Name = "宋体";//文字字体 styleTitle.Font.Size = 18;//文字大小 styleTitle.Font.IsBold = true;//粗体 //样式2 Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式 style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style2.Font.Name = "宋体";//文字字体 style2.Font.Size = 14;//文字大小 style2.Font.IsBold = true;//粗体 style2.IsTextWrapped = true;//单元格内容自动换行 style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //样式3 Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式 style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style3.Font.Name = "宋体";//文字字体 style3.Font.Size = 12;//文字大小 style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; int Colnum = dgv.Columns.Count;//表格列数 int Rownum = dgv.Rows.Count;//表格行数 //生成行1 列名行 for (int i = 0; i < Colnum; i++) { cells[0, i].PutValue(dgv.Columns[i].HeaderText); cells[0, i].SetStyle(style2); cells.SetRowHeight(0, 25); } //生成数据行 for (int i = 1; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1 + i, k].PutValue(dgv[k,i].Value); cells[1 + i, k].SetStyle(style3); } cells.SetRowHeight(1 + i, 24); } workbook.Save(path); }
时间: 2024-11-10 15:25:27