In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "how to export DataTable to Excel file in C#", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to export DataTable to Excel file in C#" can help you solve your doubts.
The conversion of DataTable to Excel file and the conversion of the last shared Excel file to DataTable are counter-operations to each other. DataTable is converted to an Excel file by passing in a parameter of type DataTable, and then converting the incoming DataTable into an Excel file. The key code for implementation is as follows:
Method 1:
/ DataTable data export Excel/// public static void DataTableExport (DataTable data, string filepath) {try {/ / Workbook book = new Workbook ("E:\\ test.xlsx"); / / Open workbook Workbook book = new Workbook (); / / create workbook Worksheet sheet = book.Worksheets [0]; / / create worksheet Cells cells = sheet.Cells / / Cell / / create a style Aspose.Cells.Style style = book.Styles [book.Styles.Add ()]; style.Borders [Aspose.Cells.BorderType.LeftBorder] .LineStyle = Aspose.Cells.CellBorderType.Thin; / / apply the left boundary line style.Borders [Aspose.Cells.BorderType.RightBorder] .LineStyle = Aspose.Cells.CellBorderType.Thin / / apply the right boundary line style.Borders [Aspose.Cells.BorderType.TopBorder] .LineStyle = Aspose.Cells.CellBorderType.Thin; / / apply the upper boundary line style.Borders.BorderType.BottomBorder] .LineStyle = Aspose.Cells.CellBorderType.Thin; / / apply the lower boundary line style.HorizontalAlignment = TextAlignmentType.Center / / horizontal alignment of cell contents style.Font.Name = "Arial"; / / font style1.Font.IsBold = true; / / set bold style.Font.Size = 11; / / set font size / / style.ForegroundColor = System.Drawing.Color.FromArgb (153,204,0); / / background color / / style.Pattern = Aspose.Cells.BackgroundType.Solid / / background style / / style.IsTextWrapped = true; / / automatic line wrapping of cell contents / / Table fill data int Colnum = number of data.Columns.Count;// table columns int Rownum = number of data.Rows.Count;// table rows / / generate row column name row for (int I = 0; I
< Colnum; i++) { cells[0, i].PutValue(data.Columns[i].ColumnName); //添加表头 cells[0, i].SetStyle(style); //添加样式 //cells.SetColumnWidth(i, data.Columns[i].ColumnName.Length * 2 + 1.5); //自定义列宽 //cells.SetRowHeight(0, 30); //自定义高 } //生成数据行 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加数据 cells[1 + i, k].SetStyle(style); //添加样式 } cells[1 + i, 5].Formula = "=B" + (2 + i) + "+C" + (2 + i);//给单元格设置计算公式,计算班级总人数 } sheet.AutoFitColumns(); //自适应宽 book.Save(filepath); //保存 GC.Collect(); } catch (Exception e) { logger.Error("生成excel出错:" + e.Message); }} 方法二: public void ToExcel(DataTable dt) { #region 3s var FilePath = @"D:\ToExcel\";//产生Excel文件路径 DeleteFile(FilePath); //创建全新的Workbook var workbook = new HSSFWorkbook();//一個sheet最多65536行 var count = 0; for (double i = 0; i < Convert.ToDouble(dt.Rows.Count) / Convert.ToDouble(65534); i++)//每个Excel文件的一个页签只能存放65536行数据 { var row_index = 0; //创建Sheet workbook.CreateSheet("Sheet" + i); //根据Sheet名字获得Sheet对象 var sheet = workbook.GetSheet("Sheet" + i); IRow row; row = sheet.CreateRow(row_index); //写入标题 for (int j = 0; j < dt.Columns.Count; j++) { row.CreateCell(j).SetCellValue(dt.Columns[j].Caption.ToString()); } row = sheet.CreateRow(++row_index); //写入数据 for (int j = 0; j < (dt.Rows.Count - count >65534? 65534: dt.Rows.Count-count); jacks +) {var r = dt.Rows [j + count]; for (int k = 0; k
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.