在承德等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),承德網(wǎng)站建設(shè)費(fèi)用合理。>using System.IO;
/// <summary> /// Get the save file full name
/// </summary> /// <remarks> /// If the file exist, then delete the file.
/// </remarks> /// <param name="fileName">save file full name, If null then open dialog to select and input save file name</param> /// <param name="fileSuffix">file Suffix,ex:"xls"</param> /// <param name="fileFilter">file filter defination ,ex:"EXCEL(*.xls)|*.xls"</param> /// <returns>except or cancel dialog return empty string, else return save file full name</returns>public static string GetSaveFileName(string fileName,string fileSuffix,string fileFilter)
{
string saveFileNameString = string.Empty;
try
{
if(fileName == null || fileName.Trim() == "")
{
//聲明保存對(duì)話框 SaveFileDiaLog dlg = new SaveFileDialog();
//默認(rèn)文件后綴 dlg.DefaultExt = fileSuffix;
//文件后綴列表 dlg.Filter = fileFilter;
//默認(rèn)路徑是系統(tǒng)當(dāng)前路徑 dlg.InitialDirectory = Directory.GetCurrentDirectory();
//打開保存對(duì)話框 if (dlg.ShowDialog() == DialogResult.Cancel) return "";
//返回文件路徑 saveFileNameString = dlg.FileName;
}
else
saveFileNameString= fileName;
if (saveFileNameString.Trim() == "")
return "";
//驗(yàn)證以fileNameString命名的文件是否存在,如果存在刪除它 FileInfo file = new FileInfo (saveFileNameString);
if(file.Exists)
{
try
{
file.Delete();
}
catch (Exception error)
{
AppMessageBox.ShowErrorNoTranslate(error.Message);
return "";
}
}
return saveFileNameString;
}
catch
{
return "";
}
}
public void CreateToExcel(string fileName)
{
string fileNameString = GetSaveFileName(fileName, "xls", "EXCEL(*.xls)|*.xls");
//驗(yàn)證strFileName 是否為空或無(wú)效 if (fileNameString.Trim() == "")
return;
//定義表格內(nèi)數(shù)據(jù)的行數(shù)和列數(shù)
Microsoft.Office.Interop.Excel.Application objExcel= null;
Workbook objWorkbook= null;//excel 工作簿 Worksheet objsheet = null; //excel sheet頁(yè) try
{
//聲明對(duì)象 objExcel = new Microsoft.Office.Interop.Excel.Application();
CultureInfo oldCulture= System.Threading.Thread.CurrentThread.CurrentCulture;
CultureInfo newCulture= new CultureInfo(objExcel.LanguageSettings.get_languageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanfuageIDUI));
System.Threading.Thread.CurrentThread.CurrentCulture= new newCulture;
//聲明excel工作簿 objWorkbook = objExcel.Workbooks.Add(Missing.Value);
//聲明sheet頁(yè) objsheet = (Worksheet)objWorkbook.ActiveSheet;
//頁(yè)面設(shè)置 objsheet.PageSetup.PagerSize = XlPagerSize.xlPagerA4; //設(shè)置A4格式 objsheet.PageSetup.BottomMargin = 0.5 / 0.035;
objsheet.PageSetup.TopMargin= 2 / 0.035;
objsheet.PageSetup.LeftMargin= 0.1 / 0.035;
objsheet.PageSetup.RightMargin= 0.05 / 0.035;
objsheet.PageSetup.Zoom= 90;
objsheet.PageSetup.CenterHorizontally= true;
string position = string.Empty;
//設(shè)置EXCEL是否可見 objExcel.Visible = false;
Microsoft.Office.Interop.Excel.Range excelRange= null;
double[] colWidth = new double[] { 3.63, 8, 13.15, 5, 14.38, 5, 11.5, 8.38, 11.5, 5, 20 };
string[] tableHeadTitle = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
//向Excel中寫入表格的表頭
//設(shè)置列水平居中和列的寬度 for (int i = 1; i <= 11; i++)
{
excelRange= objExcel.get_Range(objExcel.Cells[1,i],objExcel.Cells[50000,i]);
excelRange.HorizontalAlignment= XlHAlign.xlHAlignCenter;
excelRange.ColumnWidth= colWidth[i-1];
excelRange.Font.Size= 11;
}
//寫入標(biāo)頭 for (int i = 1; i <= 3; i++)
{
((Microsoft.Office.Interop.Excel.Range)objExcel.Cells[1,i]).Borders.Weight = 2;
objExcel.Cells[1,i] = tableHeadTitle[i - 1];
}
//合并4、5格,并填入列名:d excelRange = objExcel.get_Range(objExcel.Cells[1,4], objExcel.Cells[1,5]);
excelRange.Merge(Missing.Value);
excelRange.Borders.Weight= 2;
excelRange.set_Value(System.Reflection.Missing.Value,"d");
......
System.Threading.Thread.CurrentThread.CurrentCulture= oldCulture;
//設(shè)置EXCEL是否可見 objExcel.Visible = true;
//保存文件
}
catch (Exception error)
{
AppMessageBox.ShowExceptionNoTranslate(error.Message);
return;
}
finally
{
//關(guān)閉Excel應(yīng)用 GC.Collect();
}
}
分享標(biāo)題:私人筆記--C#導(dǎo)出Excel-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://aaarwkj.com/article22/jcpjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站排名、ChatGPT、網(wǎng)站導(dǎo)航、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容