C uses winform to simply derive Excel
- 2021-10-15 11:18:11
- OfStack
This article gives an example of how C # uses winform to simply derive Excel. Share it for your reference, as follows:
using Excel;
Introducing Excel. dll into the project
/// <summary>
/// Export Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExportExcel_Click(object sender, EventArgs e)
{
DataTable dt = this.dgvWaterTicket.DataSource;
if (dt == null)
{
return;
}
if (dt.Rows.Count == 0)
{
return;
}
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show(" Make sure your computer is installed Excel", " Prompt information ", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
xlApp.UserControl = true;
Excel.Workbooks workbooks = xlApp.Workbooks;
// Generate a new from the template workbook //Workbook workbook = workbooks.Add("D:\\aa.xls");
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];// Acquire sheet1
if (worksheet == null)
{
MessageBox.Show(" Make sure your computer is installed Excel", " Prompt information ", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
Excel.Range range;
long totalCount = dt.Rows.Count;
long rowRead = 0;
float percent = 0;
worksheet.Cells[1, 1] = frm.Text;// Exported title
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, dt.]).MergeCells = true; // Merge cells --- Number of columns
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 3]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;// Center alignment
worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]).ColumnWidth = 15; // Column width
worksheet.get_Range(worksheet.Cells[1, 2], worksheet.Cells[1, 2]).ColumnWidth = 15; // Column width
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 1]).ColumnWidth = 20; // Column width
// Write field
for (int i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[2, i + 1] = dt.Columns[i].ColumnName;
range = (Excel.Range)worksheet.Cells[2, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
}
// Write numeric value
for (int r = 0; r < dt.Rows.Count; r++)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[r + 3, i + 1] = dt.Rows[r][i];
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
//System.Threading.Thread.Sleep(500);
// Wrap lines if there are too many words. worksheet.Cells[r+1, 4] For worksheet.Cells[ Row , Column ]
worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 1, 4]).Columns.WrapText = true; // Word wrapping
worksheet.get_Range(worksheet.Cells[r + 3, 4], worksheet.Cells[r + 3, 4]).Rows.AutoFit(); // Automatic row height
//this.Text = " Export data [" + percent.ToString("0.00") + "%]...";
}
range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 2, dt.Columns.Count]);
range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
if (dt.Columns.Count > 1)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
}
xlApp.Visible = true;
}
catch
{
MessageBox.Show(" To and out Excel Failure! ", " Prompt information ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
//KillProcess("Excel");
GC.Collect();// Forcible destruction
}
}
For more readers interested in C # related content, please check the topics on this site: "Summary of C # Operating Excel Skills", "Summary of XML File Operation Skills in C #", "Tutorial on Usage of C # Common Controls", "Summary on Usage of WinForm Controls", "Tutorial on Data Structures and Algorithms of C #," Introduction to C # Object-Oriented Programming "and" Summary on Thread Use Skills of C # Programming "
I hope this article is helpful to everyone's C # programming.