狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#WinForm利用Aspose.Cells實(shí)現(xiàn)DataGridView導(dǎo)出Excel

admin
2025年2月20日 19:34 本文熱度 649

一、效果展示

二、VS2022
引用Aspose.Cells
界面設(shè)計(jì)
三、代碼實(shí)現(xiàn)
引用
using Aspose.Cells;
導(dǎo)出位置
private void button1_Click(object sender, EventArgs e){	#region   驗(yàn)證可操作性	//申明保存對(duì)話框 	SaveFileDialog dlg = new SaveFileDialog();	//默然文件后綴 	dlg.DefaultExt = "xls ";	//文件后綴列表 	dlg.Filter = "EXCEL文件(*.XLS)|*.xls ";	//默然路徑是系統(tǒng)當(dāng)前路徑 	dlg.InitialDirectory = Directory.GetCurrentDirectory();	string fileNameString = dlg.InitialDirectory + "\\printExcel.xls";
//打開保存對(duì)話框  if (dlg.ShowDialog() == DialogResult.Cancel) return; //返回文件路徑  fileNameString = dlg.FileName;
//驗(yàn)證strFileName是否為空或值無效  if (fileNameString.Trim() == " ") { return; } //定義表格內(nèi)數(shù)據(jù)的行數(shù)和列數(shù)  int rowscount = dataGridView1.Rows.Count; int colscount = dataGridView1.Columns.Count - 1; //行數(shù)必須大于0  if (rowscount <= 0) { MessageBox.Show("沒有數(shù)據(jù)可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
//列數(shù)必須大于0  if (colscount <= 0) { MessageBox.Show("沒有數(shù)據(jù)可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
//行數(shù)不可以大于65536  if (rowscount > 65536) { MessageBox.Show("數(shù)據(jù)記錄數(shù)太多(最多不能超過65536條),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
//列數(shù)不可以大于255  if (colscount > 255) { MessageBox.Show("數(shù)據(jù)記錄行數(shù)太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
//驗(yàn)證以fileNameString命名的文件是否存在,如果存在刪除它  FileInfo file = new FileInfo(fileNameString); if (file.Exists) { try { file.Delete(); } catch (Exception error) { MessageBox.Show(error.Message, "刪除失敗 ", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } #endregion

#region 導(dǎo)出Excel var dataTableTemp = DataGridViewToDataTable(dataGridView1); ToExcel(dataTableTemp, fileNameString); #endregion


MessageBox.Show(fileNameString + "\n\n導(dǎo)出完畢! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);}
DataGridView轉(zhuǎn)DataTable
/// <summary>/// DataGridView轉(zhuǎn)DataTable/// </summary>/// <param name="dataGridView"></param>/// <returns></returns>public static DataTable DataGridViewToDataTable(DataGridView dataGridView){	DataTable dt = new DataTable();	foreach (DataGridViewColumn column in dataGridView.Columns)	{		dt.Columns.Add(column.HeaderText, typeof(string)); // 假設(shè)類型以string的方式添加	}	foreach (DataGridViewRow row in dataGridView.Rows)	{		if (!row.IsNewRow)   // 忽略添加空行(通常最后一行是空行)		{			DataRow dataRow = dt.NewRow();			for (int i = 0; i < dataGridView.Columns.Count; i++)			{				dataRow[i] = row.Cells[i].Value;			}			dt.Rows.Add(dataRow);		}	}	return dt;}
數(shù)據(jù)表保存至Excel
#region 數(shù)據(jù)表保存至Excel/// <summary>/// DataTable數(shù)據(jù)表保存至Excel/// </summary>/// <param name="dt">數(shù)據(jù)源</param>/// <param name="filePath">文件完整路徑</param>public static void ToExcel(DataTable dt, string filePath){	string subTitle = string.Empty;
//新建工作簿   Workbook wb = new Workbook(); //新建工作表   Worksheet ws = wb.Worksheets[0];
ws.Name = dt.TableName; //dt.Columns.Remove("Id");
int rowIndex = 0; int colIndex = 0; int colCount = dt.Columns.Count; int rowCount = dt.Rows.Count;
ws.Cells.SetRowHeight(rowIndex, 25);//設(shè)置行高
//創(chuàng)建樣式 Style style = wb.Styles[wb.Styles.Add()];//新增樣式   style.HorizontalAlignment = TextAlignmentType.Center; //單元格內(nèi)容的水平對(duì)齊方式文字居中 style.Font.Name = "宋體"; //字體 //style.Font.IsBold = true; //設(shè)置粗體 //style.Font.Color = Color.White;//設(shè)置字體顏色 style.Font.Size = 10; //設(shè)置字體大小 //style.ForegroundColor = Color.FromArgb(0, 196, 180); //背景色 style.Pattern = BackgroundType.Solid;
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.TopBorder].Color = Color.Black; style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.BottomBorder].Color = Color.Black; style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.LeftBorder].Color = Color.Black; style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.RightBorder].Color = Color.Black;

//列名的處理 for (int i = 0; i < colCount; i++) { ws.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName); ws.Cells[rowIndex, colIndex].SetStyle(style);//給單元格關(guān)聯(lián)樣式 colIndex++; }

Style style2 = wb.Styles[wb.Styles.Add()];//新增樣式 style2.Font.Name = "宋體";//文字字體 style2.Font.Size = 10;//文字大小  style2.ShrinkToFit = true; style2.VerticalAlignment = TextAlignmentType.Center;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.TopBorder].Color = Color.Black; style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.BottomBorder].Color = Color.Black; style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.LeftBorder].Color = Color.Black; style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.RightBorder].Color = Color.Black;
rowIndex++;
for (int i = 0; i < rowCount; i++) { ws.Cells.SetRowHeight(rowIndex, 25);//設(shè)置行高 colIndex = 0; for (int j = 0; j < colCount; j++) { if (dt.Columns[j].ColumnName == "凈重(T)" || dt.Columns[j].ColumnName == "扣雜(T)" || dt.Columns[j].ColumnName == "標(biāo)準(zhǔn)水分%" || dt.Columns[j].ColumnName == "實(shí)測(cè)水分%" || dt.Columns[j].ColumnName == "扣水噸位(T)" || dt.Columns[j].ColumnName == "標(biāo)準(zhǔn)灰分%" || dt.Columns[j].ColumnName == "實(shí)測(cè)灰分%" || dt.Columns[j].ColumnName == "扣灰噸位(T)" || dt.Columns[j].ColumnName == "扣雜后凈重(T)" || dt.Columns[j].ColumnName == "結(jié)算重量(T)" || dt.Columns[j].ColumnName == "結(jié)算單價(jià)(元/T)" || dt.Columns[j].ColumnName == "結(jié)算金額(元)") { ws.Cells[rowIndex, colIndex].PutValue((dt.Rows[i][j].ToString() == "" ? 0 : Math.Round(decimal.Parse(dt.Rows[i][j].ToString()), 2)).ToString("0.00")); } else { ws.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString() == "" ? null : dt.Rows[i][j].ToString()); } if (i == rowCount - 1) { //style2.ForegroundColor = Color.Gray; style2.Pattern = BackgroundType.Solid; ws.Cells[rowIndex, colIndex].SetStyle(style2);//給單元格關(guān)聯(lián)樣式 } else { style2.ForegroundColor = Color.White; style2.Pattern = BackgroundType.Solid; ws.Cells[rowIndex, colIndex].SetStyle(style2);//給單元格關(guān)聯(lián)樣式 } colIndex++; } rowIndex++; }
//設(shè)置所有列為自適應(yīng)列寬  ws.AutoFitColumns();
for (int col = 0; col < colCount; col++) { ws.Cells.SetColumnWidthPixel(col, ws.Cells.GetColumnWidthPixel(col) + 20); }
if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath); System.IO.FileStream fs = System.IO.File.Create(filePath); fs.Close(); fs.Dispose(); wb.Save(filePath);}#endregion


閱讀原文:原文鏈接


該文章在 2025/2/21 12:26:34 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved