Kiven

Knowledge Has No Limit And Stick To It All The Time !
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

生成Excel文件方法

Posted on 2012-03-07 22:49  KivenRo  阅读(1395)  评论(0编辑  收藏  举报

使用Microsoft.Office.Interop.Excel.dll生成Excel文件:

public class CreateExcel
{
private static Microsoft.Office.Interop.Excel.Application app = null;

private static Microsoft.Office.Interop.Excel.Workbook workbook = null;

private static Microsoft.Office.Interop.Excel.Worksheet worksheet = null;

private static Microsoft.Office.Interop.Excel.Range workSheet_range = null;

/// <summary>
/// 新建Excel文件
/// </summary>
/// <param name="dataheader">添加Excel标题表(字段1:英文说明,字段二:中文说明</param>
/// <param name="dataitem">添加Excel数据表</param>
/// <param name="filepath">保存Excel文件物理路径</param>
public static void CreateNewExcel(DataTable dataheader, DataTable dataitem, string filepath)
{
//新建Excel文件

app = new Microsoft.Office.Interop.Excel.Application();

app.Visible = true;

workbook = app.Workbooks.Add(1);//创建workbook

worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];//创建worksheet

//向Excel中添加标题,第一行为英文说明,第二行为中文说明

if(dataheader!=null){
              for (int i = 0; i < dataheader.Rows.Count; i++)
{
AddExcelData(1, i + 1, dataheader.Rows[i][0].ToString(), "@", BackColumnName((i + 1).ToString()) + "1", BackColumnName((i + 1).ToString()) + "1");
}

for (int i = 0; i < dataheader.Rows.Count; i++)
{
AddExcelData(2, i + 1, dataheader.Rows[i][1].ToString(), "@", BackColumnName((i + 1).ToString()) + "2", BackColumnName((i + 1).ToString()) + "2");
}
           }


//向Excel中添加数据,数据从第三行开始

if (dataitem != null)
{
for (int i = 2; i < dataitem.Rows.Count; i++)
{
for (int j = 0; j < dataitem.Columns.Count; j++)
{
AddExcelData(i + 1, j + 1, dataitem.Rows[i][j].ToString(), "@", BackColumnName((j + 1).ToString()) + (i + 1).ToString(), BackColumnName((j + 1).ToString()) + (i + 1).ToString());
}
}
}

//保存Excel文件

workbook.SaveCopyAs(filepath);

app.Quit();
}

/// <summary>
/// 向Excel中添加数据
/// </summary>
/// <param name="row">行号</param>
/// <param name="col">列号</param>
/// <param name="data">数据</param>
/// <param name="format">单元格格式</param>
/// <param name="cell1">范围开始格</param>
/// <param name="cell2">范围结束格</param>
public static void AddExcelData(int row, int col, string data, string format, string cell1, string cell2)
{
worksheet.Cells[row, col] = data;

workSheet_range = worksheet.get_Range(cell1, cell2);

workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();

workSheet_range.NumberFormat = format;
}

/// <summary>
/// 返回Excel列标识
/// </summary>
/// <param name="code">列号</param>
/// <returns>返回列标识</returns>
public static string BackColumnName(string code)
{
string resvalue = "";

string columnlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

#region

DataTable columntable = new DataTable();

columntable.Columns.Add("code");

columntable.Columns.Add("name");

for (int i = 0; i < 256; i++)
{
if (i < columnlist.Length)
{
DataRow row = columntable.NewRow();

row["code"] = (i + 1).ToString().Trim();

row["name"] = columnlist[i].ToString().Trim();

columntable.Rows.Add(row);
}
else
{
for (int j = 0; j < columnlist.Length; j++)
{
for (int k = 0; k < columnlist.Length; k++)
{
if (i < 256)
{
DataRow row = columntable.NewRow();

row["code"] = (i + 1).ToString().Trim();

row["name"] = columnlist[j].ToString().Trim() + columnlist[k].ToString().Trim();

columntable.Rows.Add(row);
}

i++;
}
}
}
}

#endregion

for (int i = 0; i < columntable.Rows.Count; i++)
{
if (columntable.Rows[i][0].ToString().Trim() == code.Trim())
{
resvalue = columntable.Rows[i][1].ToString().Trim();
}
}

return resvalue;
}
}