Excel操作
来源:互联网 发布:淘宝网复古文艺花盆 编辑:程序博客网 时间:2024/04/27 16:23
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Windows.Forms;
namespace USTC
{
public class EXcelManager
{
public EXcelManager()
{
}
#region 导出到EXcel
private static System.Windows.Forms.DataGridView gridView;
private static System.Windows.Forms.ProgressBar ProgressBar1;
private static Timer time;
private static DataSet objSet = new DataSet();
private static SaveFileDialog saveFileDialog = new SaveFileDialog();
private static SaveFileDialog saveFileDialog2 = new SaveFileDialog();
public static System.Windows.Forms.DataGridView _gridView
{
get { return gridView; }
set { gridView = value; }
}
public static System.Windows.Forms.ProgressBar _ProgressBar1
{
get { return _ProgressBar1; }
set { _ProgressBar1 = value; }
}
public static Timer _time
{
get { return time; }
set { time = value; }
}
public static DataSet _objSet
{
get { return objSet; }
set { objSet.Clear(); objSet = value; }
}
#region 导出当前页DataGridView中的数据到EXcel中
/// <summary>
/// 导出当前页DataGridView中的数据到EXcel中
/// </summary>
/// <param name="dataGridView">dataGridView</param>
/// <param name="progreesBar">progreesBar</param>
public static void ExportTOExcel(DataGridView dataGridView, ProgressBar progreesBar)
{
ProgressBar1 = progreesBar;
gridView = dataGridView;
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
ProgressBar1.Visible = true;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "test";
//生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + gridView[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
ProgressBar1.Value += 100 / gridView.RowCount;
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
ProgressBar1.Value = 100;
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
ProgressBar1.Value = 0;
ProgressBar1.Visible = false;
}
}
}
#endregion
#region 导出整个DataGridView中的数据到Excel中
/// <summary>
/// 导出整个DataGridView中的数据到Excel中
/// </summary>
/// <param name="dataGridView">dataGridView</param>
/// <param name="progreesBar">progreesBar</param>
public static void ExportTOExcel2(DataGridView dataGridView, ProgressBar progreesBar)
{
gridView = dataGridView;
ProgressBar1 = progreesBar;
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog2.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog2.FilterIndex = 0;
saveFileDialog2.RestoreDirectory = true;
//saveFileDialog2.CreatePrompt = true;
saveFileDialog2.Title = "导出文件保存路径";
saveFileDialog2.FileName = null;
saveFileDialog2.ShowDialog();
string FileName = saveFileDialog2.FileName;
if (FileName.Length != 0)
{
ProgressBar1.Visible = true;
System.Data.DataTable dt = objSet.Tables[0];
FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine = "";
objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
ProgressBar1.Value = 0;
for (int i = 0; i < dt.Columns.Count; i++)
{
strLine = strLine + dt.Columns[i].ColumnName.ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
strLine = strLine + (i + 1) + Convert.ToChar(9);
for (int j = 1; j < dt.Columns.Count; j++)
{
strLine = strLine + dt.Rows[j].ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
ProgressBar1.Value += 100 / dt.Rows.Count;
strLine = "";
}
objStreamWriter.Close();
objFileStream.Close();
MessageBox.Show("数据已经成功导出到:" + saveFileDialog2.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
ProgressBar1.Value = 0;
ProgressBar1.Visible = false;
}
}
}
#endregion
#region 导出到XML(整个数据源)
/// <summary>
/// 导出到XML(整个数据源)
/// </summary>
public static void ExportTOXML(DataGridView dataGridView, ProgressBar progreesBar)
{
gridView = dataGridView;
ProgressBar1 = progreesBar;
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog2.Filter = "XML files (*.xml)|*.xml";
saveFileDialog2.FilterIndex = 0;
saveFileDialog2.RestoreDirectory = true;
//saveFileDialog2.CreatePrompt = true;
saveFileDialog2.Title = "导出文件保存路径";
saveFileDialog2.FileName = null;
saveFileDialog2.ShowDialog();
string FileName = saveFileDialog2.FileName;
if (FileName.Length != 0)
{
ProgressBar1.Visible = true;
objSet.WriteXml(saveFileDialog2.FileName.ToString());
for (int i = 0; i < objSet.Tables[0].Rows.Count; i++)
{
ProgressBar1.Value += 100 / objSet.Tables[0].Rows.Count;
}
MessageBox.Show("数据已经成功导出到:" + saveFileDialog2.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
ProgressBar1.Value = 0;
ProgressBar1.Visible = false;
}
}
}
#endregion
#endregion
}
}
- Java Excel 操作excel
- excel 操作
- 操作EXCEL
- 操作EXCEL
- EXCEL操作
- 操作excel
- excel操作
- Excel操作
- Excel操作
- excel操作
- Excel操作
- 操作EXCEL
- 操作excel
- excel操作
- Excel操作
- Excel操作
- Excel操作~~
- 操作EXCEL
- 解决在使用Ajax控件时出现的"Sys未定义"
- 对超过3D的多维空间的理解
- Linux的那些事儿(7)----系统数据文件和信息
- MQSeries通过MQ资源管理器建立的如队列、通道不可以在MQSC中进行管理?
- 颜色与UI
- Excel操作
- 自动化工具cppunit在bcb中的应用方法
- HTTP 500 - 内部服务器错误解决方案
- C++强制转换
- redhat linux9.0下载
- 创业第19天, 竞价助手嵌入数据库引擎
- 串口数据监测器
- 错误往往隐藏在你认为最不容易出错的地方。
- 经营网站需要办理哪些牌照资质