C#3.5を使用してExcel 2003スプレッドシートに書き込もうとしています。しかし、私はこれを異なる国の設定で正しく機能させることができません。国の設定は英語またはドイツ語です。これらの2つの設定は、10進数と数千の設定が異なります。ユーザーがオプション画面の国際設定で小数点と桁区切りを変更しない限り、すべて正常に動作します。誰も私が木の木を見ることができなくなって、明らかな何かが見当たらないと思うように助けることができますか?C# - 国際番号設定のExcel番号書式設定の問題
要約:
アクセスデータベースから取得したデータ。 c#アプリケーションで読み込み、Excelスプレッドシートに書き込む。
Excelバージョン2003 機械は英語またはドイツ語です。 Excelの[インターナショナル設定]オプションで小数点と桁区切り記号が変更されている可能性があります。これが問題の発生場所です。
観察された行動:デフォルトのオプションで
英語のセットアップ - >インターナショナル - デフォルトのオプションで期待
ドイツのセットアップなど - 小数点区切り付き> International-として期待
英語のセットアップは「」に変更し、千単位区切り記号は "。"に設定されます。 Options - > International - Excelデータが間違っているシステム区切り文字は間違っています。アスタリスク付きの行を参照してください。
データエクセル
3706888.0300 3706888.03
2587033.8000 2587033.8
2081071.1800 2081071.18
9030160.3333 90.301.603.333 **
42470.9842 424.709.842 **
4465546.2800 4465546.28
1436037.3200 1436037.32
111650.0000 111650
2567007.0833 25.670.070.833 **
この現象を示すサンプルコードがあります。もし私が何をやっているのか誰かが私に見せることができれば、それは非常に高く評価されます。
このコードサンプルを実行するには、新しいWindowsフォームアプリケーションを作成し、Form1.csに以下のコードを投稿してください。また、Microsoft.Office.Interop.Excelへの参照を追加する必要があります。
多くのおかげ
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ExportDTToExcel()
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = true;
Workbook wb = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.ActiveSheet;
string culture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();//"en-GB";
CultureInfo ci = new CultureInfo(culture);
string excelGroupSeparator = app.ThousandsSeparator.ToString();
string excelDecimalSeparator = app.DecimalSeparator.ToString();
bool systemseparators = app.UseSystemSeparators ;
if (app.UseSystemSeparators == false)
{
app.DecimalSeparator = ci.NumberFormat.NumberDecimalSeparator;
app.ThousandsSeparator = ci.NumberFormat.NumberGroupSeparator;
//ci.NumberFormat.NumberDecimalSeparator = app.DecimalSeparator;
//ci.NumberFormat.NumberGroupSeparator = app.ThousandsSeparator;
}
//app.DecimalSeparator = ci.NumberFormat.NumberDecimalSeparator;
//app.ThousandsSeparator = ci.NumberFormat.NumberGroupSeparator;
app.UseSystemSeparators = true;
// Content.
try
{
SetCellValue("3706888.0300", ws, 0, 0, ci);
SetCellValue("2587033.8000", ws, 1, 0, ci);
SetCellValue("2081071.1800", ws, 2, 0, ci);
SetCellValue("9030160.3333", ws, 3, 0, ci);
SetCellValue("42470.9842", ws, 4, 0, ci);
SetCellValue("4465546.2800", ws, 5, 0, ci);
SetCellValue("1436037.3200", ws, 6, 0, ci);
SetCellValue("111650.0000", ws, 7, 0, ci);
SetCellValue("2567007.0833", ws, 8, 0, ci);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
//wb.SaveAs(Filepath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//wb.Close(false, Type.Missing, false);
app.DecimalSeparator = excelDecimalSeparator;
app.ThousandsSeparator = excelGroupSeparator;
app.UseSystemSeparators = systemseparators;
//app.Quit();
Marshal.ReleaseComObject(app);
Marshal.ReleaseComObject(wb);
Marshal.ReleaseComObject(ws);
app = null;
wb = null;
ws = null;
}
private static void SetCellValue(string data, Worksheet ws,int row, int col, CultureInfo ci)
{
double val;
try
{
val = Convert.ToDouble(data);
Console.WriteLine(val);
}
catch (Exception e)
{
//Util.Log("Null Value ignored.", LogType.ERROR);
return;
}
try
{
string s = val.ToString();
ws.Cells[row + 2 , col + 1] = s;
//Util.Log("S:" + s, LogType.ERROR);
}
catch
{
//Util.Log("Null Value ignored.", LogType.ERROR);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
ExportDTToExcel();
this.Cursor = Cursors.Default;
}
}
}
私はちょうどあなたのコードをスキャンし、あなたがそれを使用していないので 'SetCellValue'メソッドのための入力として' CultureInfo ci'を持っているのか不思議でした... –