C# Excel出力及样式的设定

 

 

/// <summary>
/// 出力ボタン
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmdExport_ClickMain(object sender, EventArgs e)
{

// 新建Excel
Microsoft.Office.Interop.Excel.Application app = null;
Workbook wb = null;
Worksheet ws;
try
{

//设置出力的Excel的名称
saveFile.FileName = Properties.Resources.FileName;

//设置格式
saveFile.Filter = "Excel(*.xls)|*.xlsx";
string path = string.Empty;

if (saveFile.ShowDialog() == DialogResult.OK)
{
// 画面の状態を設定する
SetEnabledCtrl(FormState.Busy);

//文件保存路径

path = saveFile.FileName.ToString();

// Excel是否处于编辑状态
try
{
FileStream stream = File.OpenWrite(path);
stream.Close();
}
catch
{
MessageBox.Show(this
, CSCommon.Properties.Resources.FlexGridKAI_OutPutFile_Err
, this.Name
, MessageBoxButtons.OK
, MessageBoxIcon.Warning);
return;
}

//添加Sheet页

app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Add();
ws = (Worksheet)wb.Sheets[1];

Range rg;

//单元格赋值

ws.Cells[1, 1] = "オーダ別 組立日程表[" + chDate + "]";
rg = ws.Cells[1, 1];

// 字体加粗
rg.Font.Bold = true;

//单元格合并
rg = ws.Range[ws.Cells[1, 1], ws.Cells[1, 3]];
rg.MergeCells = true;

//对齐方式的设定
rg.HorizontalAlignment = XlHAlign.xlHAlignLeft;

//下划线

rg.Font.Underline = true;

//设置单元格为文本格式
rg.NumberFormatLocal = "@";

// 单元格背景色的设置
rg.Interior.Color = Color.FromArgb(cr.Style.BackColor.ToArgb());

// 边框线条样式设定

rg = ws.Cells[row, col];
rg.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlDouble;  //上边框设置为双框线
rg.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlDot;    // 有边框设置为虚线

rg.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous; //下边框为实线

// 生試の色を設定する
rg = ws.Cells[row + 2, col];
if (col == 1)
{

//rg.Text的值没有类型,用什么类型的值接收它,它就是什么类型
string str2 = Convert.ToString(rg.Text.Substring(0, 1));
if (str2 == Properties.Resources.star)
{

// 设置单元格里面字符串的前三位的字体
rg.Characters[0, 3].Font.Color = Color.Blue;
rg.Characters[0, 3].Font.Bold = true;
}

// 行を挿入する
rg = (Range)ws.Rows[grdData.Rows.Count - 4, Type.Missing];
rg.Insert(XlDirection.xlDown, Type.Missing);
rg = ws.Range[ws.Cells[grdData.Rows.Count - 4, 1], ws.Cells[grdData.Rows.Count - 4, grdData.Cols.Count]];
rg.Interior.Color = Color.White;

// ダイアログボックスを表示しない
app.DisplayAlerts = false;
}

#endregion

// 自动列宽
ws.Columns.AutoFit();

// 文件保存
wb.SaveAs(path);
ws = null;
wb.Close();
wb = null;
app.DisplayAlerts = false;
app.Quit();
app = null;

SetEnabledCtrl(FormState.AfterShowSomeData);
#endregion
}
}
catch (Exception ex)
{
// ファイルを閉じる
ws = null;
app.DisplayAlerts = false;
app.Quit();
app = null;

// 画面エラー状態
SetEnabledCtrl(FormState.RaiseError);
// エラーログ出力
FormLog.WriteErrorLog(ex, this.GetType().Assembly.GetName().Name);
// エラーメッセージ表示
FormLog.ShowErrorMessageBox(ex);
throw;
}
}

转载于:https://www.cnblogs.com/alwaysDoing/p/9360784.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值